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

How to add appearance stream for Annotation icons (for browser display) #337

Open
marceloandrioni opened this issue Apr 30, 2022 · 0 comments

Comments

@marceloandrioni
Copy link

marceloandrioni commented Apr 30, 2022

Hello, first off, thanks for this great library, it's very useful.

I am using the code bellow (based on this link) to attach files to a pdf. Everything works fine and the main desktop viewers (e.g. Adobe, Evince, Okular, etc) display the attachment icon correctly.
adobe_ok

Unfortunately when viewing the same file using Firefox pdf viewer the icon does not appear (but the attached file is still present):
2022-04-30_11-49

Investigating the problem I found this issue in the Firefox git. From what I could understand (I am far from a pdf specialist), the Firefox viewer assumes pdf files follow the 2.0 standard, where every annotation must have an associated appearance stream and the default icons (Graph, PushPin, Paperclip and Tag) do not exist.

As the pdf files I am creating will be mostly viewed on browsers, my question is if is possible to include an appearance stream in the pdf for the annotation icons using pikepdf (maybe copying from another file). I tried using generate_appearance_streams and get_appearance_stream but could not make it work.

Original file (no attachments): test.pdf
Attachment (just a random file): README.md
Output (annotation icon shows on the desktop viewers but not in the browser): test2.pdf

Example of a file where the annotation icon shows on the desktop viewers and in the browser: example.pdf

Thank you very much.

from pikepdf import Pdf, AttachedFileSpec, Name, Dictionary, Array
from pathlib import Path

pdf = Pdf.open('test.pdf')

filespec = AttachedFileSpec.from_filepath(
    pdf,
    Path('README.md'),
    description='This is the file description')

# https://www.pdftron.com/api/PDFTronSDK/dotnet/pdftron.PDF.Annots.FileAttachment.html
# Note that FileAttachment icons can differ in their appearance dimensions,
# so you may want to match these Rectangle dimensions or the aspect ratio
# to avoid a squished or stretched appearance :
# e_Graph : 40 x 40 e_PushPin : 28 x 40 e_Paperclip : 14 x 34 e_Tag : 40 x 32

icons = {'Graph': (40, 40),
         'PushPin': (28, 40),
         'Paperclip': (14, 34),
         'Tag': (40, 32)}

icon_name = 'PushPin'
xsize, ysize = icons[icon_name]

xmin = 100
xmax = xmin + xsize * 3
ymin = 700
ymax = ymin + ysize * 3

pushpin = Dictionary(Type=Name('/Annot'),
                     Subtype=Name('/FileAttachment'),
                     Name=Name(f'/{icon_name}'),   # Name.PushPin,
                     FS=filespec.obj,
                     Rect=[xmin, ymin, xmax, ymax],
                     Contents='This is the file description',
                     C=(1.0, 1.0, 0.0),   # color
                     T='Author',
                     M='D:20210101000000',   # modification date
                     )

pdf.pages[0].Annots = pdf.make_indirect(Array([pushpin]))

pdf.save('test2.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant