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

Font family, size, and 'style' params not working/changing in AnnotationBuilder's free_text method #1435

Closed
joerex1418 opened this issue Nov 15, 2022 · 3 comments · Fixed by #2893
Labels
workflow-annotation Everything about annotating PDF files

Comments

@joerex1418
Copy link

I'm just trying to add free text to a blank page using the AnnotationBuilder class. Some of the parameters seem to work just fine (text, rect, colors) but it looks like there is an issue with changing the font. No matter what I change it to, the font family and size, and style (bold/underline params) don't change at all.

Environment

Which environment were you using when you encountered the problem?

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

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.11.1

Code + PDF

This is a minimal, complete example that shows the issue:

from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import AnnotationBuilder

writer = PdfWriter()

writer.add_blank_page(612, 792)

annotation_1 = AnnotationBuilder.free_text(
    "This is supposed to be 20pt Courier",
    rect=(400, 500, 480, 600),
    font="Courier",
    bold=True,
    italic=True,
    font_size="20pt",
    font_color="000000",
    border_color="0000ff",
    background_color="cdcdcd",
)

annotation_2 = AnnotationBuilder.free_text(
    "This is supposed to be 36pt Helvetica",
    rect=(20, 500, 200, 600),
    font="Helvetica",
    bold=True,
    italic=True,
    font_size="36pt",
    font_color="000000",
    border_color="0000ff",
    background_color="cdcdcd",
)

writer.add_annotation(page_number=0, annotation=annotation_1)
writer.add_annotation(page_number=0, annotation=annotation_2)

with open('pdfs/output.pdf','wb') as f:
    writer.write(f)

Here is an image of the resulting PDF:

output

@KingSlayer907
Copy link

I was having this same issue and came across your post, you may moved on by now but I figured I would post my partial solution. I was able to get font family and font size working by editing the annotation builder definition file, particularly the bg_color_str code. Here is the modified code (starts on line 92), I added the font name and font size to the bg_color_str after comparing an annotation I read in vs what annotation builder was creating. I wasn't able to get bold and italic working though.

    bg_color_str = ""
    for st in hex_to_rgb(border_color):
        bg_color_str = bg_color_str + str(st) + " "
    bg_color_str = bg_color_str + "rg /"
    bg_color_str = bg_color_str + font + " " + font_size.replace("pt"," Tf")

@pubpub-zz
Copy link
Collaborator

Have to tried to do some reverse engineering creating an annotation with Acrobat Reader (or else),save it and then check how the fields are encoded with PdfReader ?

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

cbacary commented Oct 8, 2023

I am having the same issue. Furthermore, I think this should be labelled as a bug.

To reproduce:

from pypdf import PdfReader, PdfWriter
from pypdf.annotations import FreeText

writer = PdfWriter()
writer.add_blank_page(width=2200, height=2800)

# 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="50pt",
    font_color="00ff00",
    border_color="0000ff",
    background_color="cdcdcd",
)

writer.add_annotation(page_number=0, annotation=annotation)

with open('written.pdf', 'wb') as fp:
    writer.write(fp)

Changing font_size parameter changes nothing on the written pdf file.

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

Successfully merging a pull request may close this issue.

5 participants