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

FreeText annotation not showing in chrome browser #2372

Open
123woscc opened this issue Dec 27, 2023 · 6 comments
Open

FreeText annotation not showing in chrome browser #2372

123woscc opened this issue Dec 27, 2023 · 6 comments
Labels
Has MCVE A minimal, complete and verifiable example helps a lot to debug / understand feature requests is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-annotation Everything about annotating PDF files

Comments

@123woscc
Copy link

123woscc commented Dec 27, 2023

I added a FreeText annotation to a PDF, but noticed that it's not shown in some PDF viewers.

The input PDF seems not to matter.

Environment

  • Windows-10-10.0.22000-SP0
  • pypdf 3.17.4
  • pypdf==3.17.4, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=8.4.0

Code + PDF

input.pdf

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

# Fill the writer with the pages you want
pdf_path = 'input.pdf'
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

# 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",
)
writer.add_annotation(page_number=0, annotation=annotation)

# Write the annotated file to disk
with open("output.pdf", "wb") as fp:
    writer.write(fp)

output.pdf

Screenshots

The added comments are not displayed

output.pdf in firefox
image

output.pdf in chrome
image

@MartinThoma MartinThoma added workflow-annotation Everything about annotating PDF files is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF Has MCVE A minimal, complete and verifiable example helps a lot to debug / understand feature requests labels Dec 28, 2023
@MartinThoma
Copy link
Member

I can confirm that I see the annotation in Evince, but not in the Google Chrome viewer (also with a different input.pdf)

@MartinThoma
Copy link
Member

Thanks for sharing!

@stefan6419846
Copy link
Collaborator

This might be related to #2332. Did this work correctly in PyPDF2?

@pubpub-zz
Copy link
Collaborator

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

# Fill the writer with the pages you want
pdf_path = 'input.pdf'
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

You should try to append the page :

pdf_path = 'input.pdf'
reader = PdfReader(pdf_path)
writer = PdfWriter()
writer.append(reader,[0])

is it better ?

@MartinThoma
Copy link
Member

Using append causes the same problems:

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

# Fill the writer with the pages you want
pdf_path = 'input.pdf'
reader = PdfReader(pdf_path)
writer = PdfWriter()
writer.append(reader, [0])

# 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",
)
writer.add_annotation(page_number=0, annotation=annotation)


writer.create_viewer_preferences()

# Write the annotated file to disk
with open("output.pdf", "wb") as fp:
    writer.write(fp)

@MartinThoma
Copy link
Member

@stefan6419846 It was also broken in PyPDF2:

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

# Fill the writer with the pages you want
pdf_path = 'input.pdf'
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

# Create the annotation and add it
annotation = AnnotationBuilder.free_text(
    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",
)
writer.add_annotation(page_number=0, annotation=annotation)

# Write the annotated file to disk
with open("output-pypdf2.pdf", "wb") as fp:
    writer.write(fp)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has MCVE A minimal, complete and verifiable example helps a lot to debug / understand feature requests is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF workflow-annotation Everything about annotating PDF files
Projects
None yet
Development

No branches or pull requests

4 participants