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