Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-Annotation fontsize/color not working #2084

Closed
Bennse666 opened this issue Aug 11, 2023 · 12 comments
Closed

Add-Annotation fontsize/color not working #2084

Bennse666 opened this issue Aug 11, 2023 · 12 comments
Labels
workflow-annotation Everything about annotating PDF files

Comments

@Bennse666
Copy link

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
Copy link
Author

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

@MartinThoma MartinThoma added the workflow-annotation Everything about annotating PDF files label Aug 14, 2023
@kang0410
Copy link

kang0410 commented Aug 24, 2023

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

@Fellow-Bennse
Copy link

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.

@kang0410
Copy link

@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)

@Fellow-Bennse
Copy link

@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
Copy link

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

@stefan6419846
Copy link
Collaborator

stefan6419846 commented Sep 12, 2023

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.

@Fellow-Bennse
Copy link

Fellow-Bennse commented Sep 15, 2023

@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)

@ilyaZar
Copy link

ilyaZar commented Nov 22, 2023

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.

@ivoras
Copy link

ivoras commented Dec 6, 2023

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

@ilyaZar
Copy link

ilyaZar commented Dec 6, 2023

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

@stefan6419846
Copy link
Collaborator

Should be fixed by #2893.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
workflow-annotation Everything about annotating PDF files
Projects
None yet
Development

No branches or pull requests

7 participants