pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

Add-Annotation fontsize/color not working

Open Bennse666 opened this issue 1 year ago • 11 comments

So, I'm trying to build a system to add/edit/delete Annotations on PDF. But the Annotations do not use the given fontsize nor fontcolor. And when I set border_color to 000000 I don't get any Text either, just the white "Annotation-Area" (white because of the background-color).

$ python -m platform
macOS-12.6-x86_64-i386-64bit

$ python -c "import pypdf;print(pypdf.__version__)"
3.14.0
def dec_to_hex(num):
    number = str(hex(int(num)))
    if number.startswith('0x'):
        number = number[2:]
    if number=='0':
        number='00'
    return number

def add_annotation_pdf(annotation, temp_file_path):
    r, g, b = annotation['rgb'].replace('(','').replace(')','').split(',')
    rgb_str = f'{dec_to_hex(r)}{dec_to_hex(g)}{dec_to_hex(b)}'
    
    reader = PdfReader(temp_file_path)
    page = reader.pages[annotation['page']]
    page.scale_to(annotation['overall_x'], annotation['overall_y'])
    writer = PdfWriter()
    writer.add_page(page)

    annot = AnnotationBuilder.free_text(
        text=annotation['note'],
        rect=(annotation['x1'], annotation['y1'], annotation['x2'], annotation['y2']),
        font='Arial',
        italic=True,
        bold=True,
        font_size=str(annotation['fontSize'])+'pt',
        font_color=rgb_str,
        background_color='ffffff',
        border_color='ffffff'
    )

    writer.add_annotation(page_number=annotation['page'], annotation=annot)

    with open(temp_file_path, 'wb') as pdf_obj:
        writer.write(pdf_obj)

No Traceback Error As for the PDF(s).. it's for every PDF

Bennse666 avatar Aug 11 '23 12:08 Bennse666

Quick Info: changing border_color ALSO influences font_color, but font_color is not being used at all (it's being ignored)

Bennse666 avatar Aug 11 '23 12:08 Bennse666

I've got the same issue so am following.

-- Update The problem appears to be due to the mac default preview app, not pypdf. When I viewed the pdf in another viewer (Acrobat reader), the annotation formatting is fine. Only when I save the document in Acrobat Reader, are the annotations displayed appropriately in Mac Preview. Not sure if there's a solution to make this display correctly in Mac Preview

kang0410 avatar Aug 24 '23 10:08 kang0410

Any update on this matter?

@kang0410 for me it's not related to default viewer as I am not even using the preview-app. I am using the function(s) for a webbased application where it also shows the PDF. After creating the Stamp/Annotation on the document, we use another library to create an image of the annotated page, and even there the Stamp/Annotation is not being formatted/displayed correctly.

Fellow-Bennse avatar Aug 28 '23 06:08 Fellow-Bennse

@Fellow-Bennse - Strange, have you tried opening the document in Acrobat reader (both after the stamp/annotation and after the other library to create an image)

kang0410 avatar Aug 28 '23 10:08 kang0410

@kang0410 Yea I did, and the annotations are missing Font-Size and Font-Color When I set Border-Color to White (because of me not wanting a Border), then the Text is also white, since it uses the Border-Color for the Font for whatever reason

Fellow-Bennse avatar Sep 04 '23 09:09 Fellow-Bennse

I would really appreciate it if it could be looked into..

Fellow-Bennse avatar Sep 12 '23 13:09 Fellow-Bennse

I cannot help you with the issue itself, but providing mostly minimal, but executable code as well as a simple PDF showing your issue usually makes it much easier to work on such issues (for now, one has to figure out how to actually call your code before doing actual debugging). Additionally, AnnotationBuilder.free_text raises a deprecation warning and should be replaced with the correct alternative.

stefan6419846 avatar Sep 12 '23 14:09 stefan6419846

@stefan6419846 Hey Hey, yea I swapped back to from pypdf.annotations import FreeText because of the deprecation-warning

Showing the issue is a little hard when the annotations font-color is the same as border-color, meaning if I set Border-color to ffffff (white), I am also unable to see the text)

As for font-size it's kinda the same thing. If I put it 50, it's still default-size from the library, hard to like, you know, properly show it.

As for executable code:

from pypdf.annotations import FreeText

reader = PdfReader('your_pdf_path')
page = reader.pages[0]
page.scale_to(1600, 2300)
writer = PdfWriter()
writer.add_page(page)

annot =FreeText(
        text='Test-Text,
        rect=(50, 1700, 200, 1500),
        font='Arial',
        italic=True,
        bold=True,
        font_size='30pt',
        font_color='eeeeee',
        background_color='ffffff',
        border_color='ffffff'
    )
writer.add_annotation(page_number=0, annotation=annot)

with open('your_pdf_path', 'wb') as pdf_obj:
    writer.write(pdf_obj)

Fellow-Bennse avatar Sep 15 '23 07:09 Fellow-Bennse

I have the exact same issue. border_color overwrites font_color, when "white" is specified as above nothing occurs (the font ending up in white as above). If not setting border_color at all then I cannot set font_color to anything different since the class default value field of border_color is used instead (which is black), so one always ends up with the same border and font color.

ilyaZar avatar Nov 22 '23 10:11 ilyaZar

I have the same problem: font_color is ignored in favor of border_color, and font_size is completely ignored.

ivoras avatar Dec 06 '23 11:12 ivoras

Maybe as a reproducible error @stefan6419846 , one can just look at the documentation https://pypdf.readthedocs.io/en/latest/user/adding-pdf-annotations.html#free-text where the snippet

# Create the annotation and add it
annotation = FreeText(
    text="Hello World\nThis is the second line!",
    rect=(50, 550, 200, 650),
    font="Arial",
    bold=True,
    italic=True,
    font_size="20pt",
    font_color="00ff00",
    border_color="0000ff",
    background_color="cdcdcd",
)

indicates the use of different colors (font_color="00ff00" and border_color="0000ff") but both is displayed in blue.

image

ilyaZar avatar Dec 06 '23 13:12 ilyaZar