Welcome, guest | Sign In | My Account | Store | Cart

Just another demonstration of PyMuPDF's features to deal with annotations:

Take a page with several annotations and let them change places in reverse order: first and last annot exchange their rectangles, second and second to last, etc.

The annotation images are enlarged or compressed as required to fit into their new areas.

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import fitz
doc = fitz.open("some.pdf")      # open pdf
page = doc[n]                    # open the page (0-based number)
rtab = []                        # store all rectangles here
annot = page.firstAnnot          # read first annotation
while annot:
	rtab.append(annot.rect)  # store rectangle
	annot = annot.next       # read next annot

annot = page.firstAnnot          # cycle thru annots again
for rect in reversed(rtab):
	annot.setRect(rect)      # give it a new place
	annot = annot.next       
	
doc.save("some-reversed.pdf")    # save PDF with reversed annotations