pypdf
pypdf copied to clipboard
PolyLine annotation is not visible
I'm trying to add a polyline annotation to a pdf file. It does not work whether it is an existing file or a new file (add_blank_page).
Environment
Which environment were you using when you encountered the problem?
$ python -m platform
macOS-12.6.1-arm64-arm-64bit
$ python -c "import pypdf;print(pypdf.__version__)"
3.8.0
Code + PDF
This is a minimal, complete example that shows the issue:
from pypdf import PdfWriter
from pypdf.generic import AnnotationBuilder
writer = PdfWriter()
writer.add_blank_page(200, 200)
annotation = AnnotationBuilder.polyline(
vertices=[
(50, 50),
(50, 150),
(150, 150),
(150, 50),
(50, 50),
],
)
writer.add_annotation(page_number=0, annotation=annotation)
with open("output.pdf", "wb") as f:
writer.write(f)
The output pdf file is empty: output.pdf
Please share the output of "pdfly pagemeta" https://pypi.org/project/pdfly/ of the first page of that document
output.pdf, page index 0
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Attribute ┃ Value ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ mediabox │ (0, 0, 200, 200): with=200 x height=200 │
│ cropbox │ (0, 0, 200, 200): with=200 x height=200 │
│ artbox │ (0, 0, 200, 200): with=200 x height=200 │
│ bleedbox │ (0, 0, 200, 200): with=200 x height=200 │
│ annotations │ 1 │
└─────────────┴─────────────────────────────────────────┘
All annotations:
1. /PolyLine at [50, 50, 50, 50]
If you look at the pdf, you will see the polyline. To make it visible change the color. The problem is just the default color. Set it to red ie. ArrayObject(FloatObject(1.0),FloatObject(0.0),FloatObject(0.0))
and it should be good
Interesting, I would have thought that black is the default color.
Here is a complete example:
from pypdf import PdfWriter
from pypdf.generic import AnnotationBuilder, NameObject, FloatObject, ArrayObject
writer = PdfWriter()
writer.add_blank_page(200, 200)
annotation = AnnotationBuilder.polyline(
vertices=[
(50, 50),
(50, 150),
(150, 150),
(150, 50),
(50, 50),
],
)
# Change the border color of the PolyLine:
annotation[NameObject("/C")] = ArrayObject(
[FloatObject(0.9), FloatObject(0.1), FloatObject(0)]
)
writer.add_annotation(page_number=0, annotation=annotation)
with open("output.pdf", "wb") as f:
writer.write(f)
From the specs:
It seems that the problem is on the side of the visualization of the pdf on Mac. I don't see the annotation using either Preview or Chrome, but I do see it using an online pdf reader.
Is the new code I shared also causing those issues or is it just the old code?
The code you shared did not solve the issue. If that helps, I also tried with the rectangle annotation and had a better result:
from pypdf import PdfWriter
from pypdf.generic import AnnotationBuilder
writer = PdfWriter()
writer.add_blank_page(200, 200)
# Add the rectangle
annotation = AnnotationBuilder.rectangle(
rect=(50, 150, 150, 50),
)
writer.add_annotation(page_number=0, annotation=annotation)
with open("output.pdf", "wb") as f:
writer.write(f)
The generated pdf has a visible annotation on Chrome but not on Preview (which seems to be known for poor annotation support).
if the issue in on Preview, I think we can close this issue
Let's leave it open to either document the color-thing or change the default value (which is blocked by the annotation PR https://github.com/py-pdf/pypdf/pull/1745). This will be pretty easy to close once the annotation PR is merged (I now hope for mid of May)