pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

PolyLine annotation is not visible

Open femtozer opened this issue 1 year ago • 9 comments

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

femtozer avatar Apr 19 '23 17:04 femtozer

Please share the output of "pdfly pagemeta" https://pypi.org/project/pdfly/ of the first page of that document

MartinThoma avatar Apr 19 '23 19:04 MartinThoma

                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]

femtozer avatar Apr 19 '23 22:04 femtozer

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

pubpub-zz avatar Apr 22 '23 11:04 pubpub-zz

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:

image

MartinThoma avatar Apr 22 '23 16:04 MartinThoma

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.

femtozer avatar Apr 23 '23 14:04 femtozer

Is the new code I shared also causing those issues or is it just the old code?

MartinThoma avatar Apr 23 '23 19:04 MartinThoma

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

femtozer avatar Apr 24 '23 07:04 femtozer

if the issue in on Preview, I think we can close this issue

pubpub-zz avatar Apr 24 '23 21:04 pubpub-zz

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)

MartinThoma avatar Apr 25 '23 05:04 MartinThoma