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

Version 1.10.0 of PyMuPDF supports PDF annotations. Among other things they can be extracted as images and also updated to some extent.

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# loop through annotations of a page, change their author,
# make them 10% larger and save an image as PNG file
import fitz
doc    = fitz.open("some.pdf") # open the PDF
page   = doc[n]                # access page n (0-based)
annot  = page.firstAnnot       # get first annotation
matrix = fitz.Matrix(1.1, 1.1) # define matrix for scaling +10%
i      = 0                     # counter for file idents

# loop through the page's annotations
while annot:
    pix = annot.getPixmap(matrix = matrix)    # picture map for the annot
    pix.writePNG("annot-%s.png" % (str(i),))  # save it as PNG
    i += 1                                    # increase counter
    d = annot.info                            # get annot's info dictionary
    d["title"] = "Jorj X. McKie"              # set author (= popup title)
    annot.setInfo(d)                          # update info dict in annot 
    r = annot.rect * matrix                   # scale annot's rectangle
    annot.setRect(r)                          # store rect back in annot
    annot = annot.next                        # get next annot on page

# update PDF with the changes 
doc.saveIncr()
# alternatively, save to a new file:
doc.save("some-updates.pdf")

PyMuPDF runs on Python versions 2.7 and 3.1 through 3.6, 32bit and 64bit, on Windows, Linux, Mac. For Windows, binaries exist if you want to avoid generating MuPDF yourself (required otherwise).