py-pdf / pypdf

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files
https://pypdf.readthedocs.io/en/latest/
Other
8.05k stars 1.39k forks source link

FreeText annotation not showing in chrome browser #2372

Open 123woscc opened 8 months ago

123woscc commented 8 months ago

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

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 commented 8 months ago

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

MartinThoma commented 8 months ago

Thanks for sharing!

stefan6419846 commented 8 months ago

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

pubpub-zz commented 8 months ago
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 commented 8 months ago

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 commented 8 months ago

@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)
patrickswelsh commented 3 months ago

I am experiencing the same issue.

pubpub-zz commented 3 weeks ago

I've found the problem : the annotation is missing the ["/AP"]["/N"] entry. under analysis to produce the form