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
Diff to Previous Revision
--- revision 1 2016-12-13 22:18:03
+++ revision 2 2017-01-22 14:02:16
@@ -7,12 +7,9 @@
rtab.append(annot.rect) # store rectangle
annot = annot.next # read next annot
-i = len(rtab)-1 # point to last rectangle
-
annot = page.firstAnnot # cycle thru annots again
-while annot:
- annot.setRect(rtab[i]) # give it a new place
- i -= 1 # count backwards
+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