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

PolyLine annotation is not visible #1805

Open
femtozer opened this issue Apr 19, 2023 · 9 comments · May be fixed by #3014
Open

PolyLine annotation is not visible #1805

femtozer opened this issue Apr 19, 2023 · 9 comments · May be fixed by #3014
Labels
nf-documentation Non-functional change: Documentation workflow-annotation Everything about annotating PDF files

Comments

@femtozer
Copy link

femtozer commented Apr 19, 2023

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 femtozer changed the title Rectangle/Polyline annotation not working Rectangle/Polyline annotation not working (on M1 mac?) Apr 19, 2023
@femtozer femtozer changed the title Rectangle/Polyline annotation not working (on M1 mac?) Polyline annotation not working (on M1 mac?) Apr 19, 2023
@femtozer femtozer reopened this Apr 19, 2023
@MartinThoma
Copy link
Member

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

@femtozer
Copy link
Author

                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]

@pubpub-zz
Copy link
Collaborator

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

@MartinThoma
Copy link
Member

MartinThoma commented Apr 22, 2023

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 MartinThoma changed the title Polyline annotation not working (on M1 mac?) PolyLine annotation is not visible Apr 22, 2023
@femtozer
Copy link
Author

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.

@MartinThoma
Copy link
Member

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

@femtozer
Copy link
Author

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

@pubpub-zz
Copy link
Collaborator

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

@MartinThoma
Copy link
Member

Let's leave it open to either document the color-thing or change the default value (which is blocked by the annotation PR #1745). This will be pretty easy to close once the annotation PR is merged (I now hope for mid of May)

@MartinThoma MartinThoma added nf-documentation Non-functional change: Documentation workflow-annotation Everything about annotating PDF files labels Apr 25, 2023
stefan6419846 added a commit to stefan6419846/pypdf that referenced this issue Dec 20, 2024
@stefan6419846 stefan6419846 linked a pull request Dec 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nf-documentation Non-functional change: Documentation workflow-annotation Everything about annotating PDF files
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants