0

This issue arises because the entire PDF file cannot be disclosed to the client. After I cut off the pdf, however this seems to just hide as the size is still the same and the cutout part can still be called out

First of all, I decided to share the part that can be revealed. Then create a blank page and paste the part that can be disclosed

from PyPDF2 import PdfWriter, PdfReader

reader = PdfReader("text6.pdf") 
writer = PdfWriter()


page1 = reader.pages[0]
page1.mediaBox.upper_right = (page1.mediaBox.right/2, page1.mediaBox.top/2 )
page1.cropBox.upper_right = (page1.cropBox.right/2, page1.cropBox.top/2 )
print(page1.mediaBox.getWidth(),page1.mediaBox.getHeight())

Blank_page = PageObject.createBlankPage(None, page1.mediaBox.getWidth(), page1.mediaBox.getHeight())
print(Blank_page.mediaBox.getWidth(),Blank_page.mediaBox.getHeight())

Blank_page.mergePage(page1)

writer.addPage(Blank_page)

with open("PyPDF2-output.pdf", "wb") as fp:
    writer.write(fp)
Ray
  • 1
  • 2
  • Using PyMuPDF, you can "physically" erase content using redaction annotations. This removes text and (parts of) images overlapping a given rectangle. So for a rectangle you want to **_keep,_** you define the (up to) four rectangles representing the rest of the page (left, top, right, bottom) and clear their contents. When done, save the PDF with an option, that removes the cleaned parts physically. Ping me if you need help. – Jorj McKie Mar 23 '23 at 10:52

0 Answers0