1

I'm new to borb and PDF manipulation. I'm trying to add a QR code (or any other image) to a PDF file, which I could do simply using the pypdf and reportlab libraries. But what I really need add the images as annotations (it would work using either borb or the other libraries I mentioned).

I tried searching for examples and reading the documentation of pypdf and borb in order to creating image annotations, but could not find a single example.

Of all the examples I saw, none of them (borb or pypdf) tells how to add images as annotations: I did find how to add lines, squares, free text, stamps, etc, but not images.

Is that even possible?

Can anyone provide an example in borb or pydfb/reportlab (or in any other library), please?

I might try to create an annotation "manually" if I knew the dictionary keys necessary to do so according to the PDF specification (which is quite complicated)...

Thanks in advance :)

SheikStack
  • 11
  • 4

1 Answers1

0

I looked too and see no current facility in Borb, but
do double check PyPDF https://pypdf.readthedocs.io/en/stable/user/add-watermark.html
or better yet PyMuPDF as the feature below is using MuPDF core. Sorry currently that one is "SIGNATURE read only."

This task is usually done in a GUI PDF Viewer/Editor as it often requires a "Hand and Eye" Co-ordination, for example here I have taken the image from upper right and am positioning a copy lower to the left.

enter image description here

Now it is possible to replicate that ability in a programmable way either

  • Export & Import as a PNG or other bitmap and place at X,Y co-ordinates, this is what is happening in the viewer it is clipboard copy and paste.
  • Save the image externally as a PDF and use as a command line PDF Stamp Annotation, there are many good examples of that time honoured method.
    Newer X-Platform apps include coherent cpdf or https://pdfcpu.io/core/stamp
  • Use an API with lots of code looking for the image to extract then a space and scale to place the image in an unknown page area. This will be custom built by you.

Note that when adding signature stamps as pure image, neither is classed as a comments annotation, one is simply considered part of the body objects and the other is imbedded along with the signature object as part of the signing process.

enter image description here

So adding custom image stamps is generally done as part of a GUI editor function, and whist allowed in a signed file, if done after the signing event it will compromise the signature validity by showing as a modification after signing.

enter image description here

You asked how such annotation is added to the PDF at a code level and for that example it is 4 extra objects. In effect this is a mini page definition as a stamp annotation. Here the page with its image is scaled as 20.25 itself but placed at a different scale within a Rect[131.35652 759.5236 207.29605 835.4632] thus roughly 76 points across and high.

22 0 obj
<</AP<</N 24 0 R>>/C[1 0 0]/CreationDate(D:20230706154555+01'00')/F 4/IT/Stamp/M(D:20230706154624+01'00')/Name/#23MD#23heDuYH5QiX5hL66UJ80/NM(1c6634d9-eebe-4efe-86ade125b32e5086)/P 6 0 R/Popup 23 0 R/Rect[131.35652 759.5236 207.29605 835.4632]/Subj(New Stamp_1)/Subtype/Stamp/T(lez)/Type/Annot>>
endobj

23 0 obj
<</F 28/M(D:20230706154555+01'00')/NM(1d640ce6-a043-4856-adf72fbd90acbdce)/Open true/P 6 0 R/Parent 22 0 R/Rect[216.73766 757.8895 374.23768 835.1188]/Subtype/Popup/Type/Annot>>
endobj

24 0 obj
<</BBox[0 0 20.25 20.25]/Type/XObject/Length 39/Resources<</XObject<</Im0 25 0 R>>>>/Subtype/Form>>
stream
q
20.25 0 0 20.25 0 0 cm
/Im0 Do
Q
q
Q

endstream
endobj

25 0 obj
<</BitsPerComponent 8/ColorSpace/DeviceRGB/Subtype/Image/Width 27/Height 27/Length 2187>>
stream
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ....continues for the 2187 Bytes image

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
  • Thanks KJ for your reply. I got one question: does any of the suggestions you made add the image as an annotation, without altering the "original" PDF content? The idea is to add visual cues of some sort of "signatures" but allow to recalculate the original PDF content hash, no matter how many "evidences" (stamps, attached files, etc) where added. – SheikStack Jul 03 '23 at 13:07
  • Thanks again, KJ. But going back to the basics... Do you know of a way to add an "image annotation"? For instance, add a QR code image but as an annotation (all our visual cues will be added and controlled by our website, and at the end of the process it will apply a final signature that will "lock" the PDF, preventing any further modifications. I've read the docs and some code of pypdf and borb, but to write my own code I'm needing the proper key-value pairs for the PDF dictionary according to the official PDF specs, which could not find either. – SheikStack Jul 03 '23 at 14:10
  • Thanks so much for your help, KJ. I'll check the resources you gave me :) – SheikStack Jul 03 '23 at 17:59
  • Thank you so much JK!!! You're awesome! – SheikStack Jul 07 '23 at 18:36