5

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.

Harry Cutts
  • 1,352
  • 11
  • 25
n33
  • 383
  • 4
  • 13
  • 1
    According to [PyPDF2 documentation](https://pythonhosted.org/PyPDF2/PdfFileWriter.html) the `PdfFileWriter` has a `removeLinks()` method that "removes links and annotations." Can you use that? – Trevor Reid Mar 15 '19 at 01:15
  • This works for me, thanks! @TrevorReid – n33 Mar 15 '19 at 02:12
  • Great. Glad it helped @n33. I submitted that as an answer based on your success and a little more testing of my own. – Trevor Reid Mar 15 '19 at 02:18

1 Answers1

4

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.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46