n33
n33

Reputation: 433

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.

Upvotes: 6

Views: 4798

Answers (2)

magical_psy
magical_psy

Reputation: 11

for anyone using PyPDF2-3.0.1,this will work fine for you

import PyPDF2

with open('input.pdf', 'rb') as pdf_obj:
    pdf = PyPDF2.PdfReader(pdf_obj)
    out = PyPDF2.PdfWriter()
    for page in pdf.pages:
        if page.annotations:
            page.annotations.clear()
        out.add_page(page)
with open('output.pdf', 'wb') as f: 
    out.write(f)

Upvotes: 1

Douglas Reid
Douglas Reid

Reputation: 3788

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.

Upvotes: 4

Related Questions