How can I transfer annotations between PDFs (e.g. using pymupdf)

380 Views Asked by At

I have been looking through the pymupdf documentation, and while there is a lot there and I can see how to identify annotations (Annot class), I can't work out how to put an annotation that I have found in one document from that one into another. Has anyone tried this before?

I am trying to merge markups generated in BlueBeam at different times into one document. I don't need to move the annotations around, just to transfer them. If I can assign them to different layers or edit the "name" attribute, that would also be good, but I can probably work out how to do that after they are transferred.

1

There are 1 best solutions below

0
On

JorjMcKie has answered this over on the PyMuPDF repository, Issue 1310. There is no direct way to merge markups in the way that I would like to do it, but Jorj has demonstrated a way that involves extracting the source data and placing it in the new file (details copied from his response below).

You can freely access all PDF source identified by an xref via doc.xref_object(annot.xref, compressed=True/False). "compressed" False gives you the syntax like above. True will deliver a string without newliners and unnecessary spaces. Likewise you can access parts of that source via doc.xref_get_key(annot.xref, "Subtype"), which will return ("name", "/Polygon") in the above case. This latter feature also supports dictionary hierarchies: doc.xref_get_key(annot.xref, "AP/N") will give you the xref string of the normal appearance dictionary in the form ("xref", "38 0 R").

Vice versa it is possible to use PyMuPDF like an editor for these things:

doc.update_object(annot.xref, modified_source) using some modified postscript version of above doc.xref_set_key(annot.xref, "C", "[1 0 0]") which in this case will change the stroke color from blue to red.

All this also works for the PDF trailer: just use -1 instead of a positive xref. A unique chance to radically spoil your PDF