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

Add Ink Annotation Support #1959

Open
AndrewADev opened this issue Jul 9, 2023 · 7 comments
Open

Add Ink Annotation Support #1959

AndrewADev opened this issue Jul 9, 2023 · 7 comments
Labels
is-feature A feature request workflow-annotation Everything about annotating PDF files

Comments

@AndrewADev
Copy link

Explanation

I was recently using pypdf to process some documents, when I noticed that it will extract and output information on the "Ink" annotation type (used by Adobe when 'highlighting' scanned documents), but does not yet directly support it. (The active development going on for Annotations looks to involve only refactoring at the moment)

So, would it be possible to support the Ink annotation type directly? (See below for my attempt at a hand-rolled version)

Thanks for keeping a handy tool like this open source and alive!

Code Example

Usage would be analogous to the other Annotations.

For the new API, probably something like this:

annotation = Ink(
    ink_list = [[499, 
        610, 
        500,
        610,
        502, 
        610 
    ]],
    rect = [485, 596, 555, 624],
    # optional
    highlight_color= "fcff30"
)

For the soon to be deprecated AnnotationBuilder, it would probably be along the lines of:

from pypdf.generic import AnnotationBuilder

annotation = AnnotationBuilder.ink(
    ink_list = [[499, 
        610, 
        500,
        610,
        502, 
        610 
    ]],
    rect = [485, 596, 555, 624],
    # optional
    highlight_color= "fcff30"
)

Other things tried

Note that the following snippet seems to get an annotation on the page where I expect it to be, but I am unable to keep it from appearing too small and faint (analogous to #1904, though the solution using "/Border" causes inconsistent opacity/visibility of the underlying document), so there seems to be something missing - possibly one of the other annotation properties from my example file (nothing seems obvious) or something is slightly malformed:

from typing import List, Tuple, Union
from pypdf.generic import ArrayObject, DictionaryObject, FloatObject, NameObject, RectangleObject, hex_to_rgb

# You can add the result as a normal annotation
def ink_annotation(ink_list: List[List[float]], rect: Union[RectangleObject, Tuple[float, float, float, float]], highlight_color: str = "fcff30"):
    ink_obj = DictionaryObject(
        {
            NameObject("/Type"): NameObject("/Annot"),
            NameObject("/Subtype"): NameObject("/Ink"),
            NameObject('/IT'): NameObject('/InkHighlight'),
            NameObject('/Rect'): RectangleObject(rect),
            NameObject('/InkList'): ArrayObject(
                [ArrayObject([FloatObject(pt) for pt in path]) for path in ink_list]
            ),
            NameObject('/C'): ArrayObject(
                [FloatObject(n) for n in hex_to_rgb(highlight_color)]
            ),
            # This will keep the highlight from appearing too faint (issue analogous to #1904),
            # but visibility of the text is then inconsistent across viewers (just fine in Adobe,
            # but underlying document no longer visible when opened in Chrome, where it no longer
            # behaves like a 'highlight').
            #
            # Note that a "/Border" object does not seem to be present in the original document, where
            # the annotation correctly displays as a 'highlight' in both Adobe and Chrome. 
            # 
            # Example based on: https://github.com/py-pdf/pypdf/issues/1904#issuecomment-1601242890
            #NameObject('/Border'): ArrayObject([FloatObject(0), FloatObject(0), FloatObject(7.0)])
        }
    )
    return ink_obj

Unfortunately for my current use case, I don't know which viewer will be used to open the documents, so that doesn't quite work for me. Perhaps the example is helpful for others and/or for a future implementation, though.

@MartinThoma MartinThoma removed their assignment Aug 14, 2023
@MartinThoma MartinThoma added workflow-annotation Everything about annotating PDF files is-feature A feature request labels Aug 14, 2023
@themarisolhernandez
Copy link

Any updates on this?

@stefan6419846
Copy link
Collaborator

Feel free to give it a try yourself and submit a corresponding PR.

@themarisolhernandez
Copy link

I was having problems adding Ink annotations back to a PDF using PdfWriter.add_annotation(). I think the problem is related to the PDF viewer. When I open the file after adding the Ink annotation in Mac's preview app, the Ink annotation is transparent. The file and annotation look fine when viewing it under AdobeReader.

Any ideas why?

@pubpub-zz
Copy link
Collaborator

I was having problems adding Ink annotations back to a PDF using PdfWriter.add_annotation(). I think the problem is related to the PDF viewer. When I open the file after adding the Ink annotation in Mac's preview app, the Ink annotation is transparent. The file and annotation look fine when viewing it under AdobeReader.

Any ideas why?

duplicate of #2332

@stefan6419846
Copy link
Collaborator

@AndrewADev Are you still interested in adding a corresponding PR?

@AndrewADev
Copy link
Author

@AndrewADev Are you still interested in adding a corresponding PR?

Hey @stefan6419846 , sorry, I thought from your earlier comment maybe the other commenter was interested.

If someone is interested in picking this up right this moment, I think they could do so.

If there aren't any interested parties, I may try to take a look over the next week or so.

@stefan6419846
Copy link
Collaborator

No worries - I just went through the backlog of old issues today to check whether they are still relevant. The other comments are older than a year now, thus I do not think that something will happen about this.

This being said: If you want, you are of course invited to propose a corresponding PR, but I will not (and cannot) force anyone to work on this ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-feature A feature request workflow-annotation Everything about annotating PDF files
Projects
None yet
Development

No branches or pull requests

5 participants