Search code examples
pythonpython-3.xpypdf

How to remove annotations in pdf in Python 3


My original goal was to remove the extensive white margins on my PDF pages.

Then I found this purpose can be achieved by scaling the page using the code below, but annotations are not scaled.

import PyPDF2

# This works fine
with open('old.pdf', 'rb') as pdf_obj:
    pdf = PyPDF2.PdfFileReader(pdf_obj)
    out = PyPDF2.PdfFileWriter()
    for page in pdf.pages:
        page.scale(2, 2)
        out.addPage(page)
    with open('new.pdf', 'wb') as f: 
        out.write(f)

# This attempts to remove annotations
with open('old.pdf', 'rb') as pdf_obj:
    pdf = PyPDF2.PdfFileReader(pdf_obj)
    page = pdf.pages[2]
    print(page['/Annots'], '\n\n\n\n')
    page.Annots = []
    print(page['/Annots'])

Is there a way to remove annotations? Or any suggestion that can help me to get rid of the white margin.


Solution

  • The method PdfFileWriter.removeLinks() removes links and annotations. So, if you are okay with losing both you can add out.removeLinks() in your first block of code, the one that's working fine.