Sure! To modify your program to keep any bookmarks (also known as outlines) present in the PDF, you need to ensure that the bookmarks are copied from the original PDF to the new PDF. In your current code, you're only copying the pages, which doesn't include the bookmarks.
To include the bookmarks, you can use the clone_document_from_reader method provided by PdfWriter. This method clones the entire document structure, including pages, bookmarks, and other objects. Then, you can remove the digital signatures by manipulating the PDF's structure to remove signature fields.
Here's the modified code:
import sys
from pypdf import PdfReader, PdfWriter
def remove_digital_signature(input_file, output_file):
reader = PdfReader(input_file)
writer = PdfWriter()
# Clone the entire document structure, including bookmarks
writer.clone_document_from_reader(reader)
# Remove digital signatures by removing signature fields from AcroForm
if "/AcroForm" in writer._root_object:
acro_form = writer._root_object["/AcroForm"]
if "/Fields" in acro_form:
fields = acro_form["/Fields"]
# Filter out signature fields (fields with /FT (field type) of /Sig)
fields_to_keep = []
for field in fields:
field_obj = field.get_object()
if field_obj.get("/FT") != "/Sig":
fields_to_keep.append(field)
acro_form.update({"/Fields": fields_to_keep})
# Optionally remove the /SigFlags entry
if "/SigFlags" in acro_form:
del acro_form["/SigFlags"]
with open(output_file, "wb") as fp:
writer.write(fp)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python pdf_signature_remover.py input.pdf output.pdf")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
remove_digital_signature(input_file, output_file)
print(f"PDF created successfully with digital signature removed: {output_file}")
Explanation:
Notes:
-
Ensure Correct Version of PyPDF: Make sure you have the latest version of PyPDF installed, as older versions may not support some of these methods. You can install or upgrade PyPDF using:
pip install --upgrade pypdf
-
Handling Encrypted PDFs: If your PDF is encrypted, you'll need to decrypt it first before processing. You can decrypt it using:
if reader.is_encrypted:
reader.decrypt(password) # Replace 'password' with the actual password
-
Removing Links vs. Signatures: The original code uses writer.remove_links(), which removes hyperlinks and other types of links in the PDF. Since we're specifically removing digital signatures, we've modified the code to remove signature fields from the /AcroForm instead.
Testing the Code:
- Test the modified script with a PDF that contains bookmarks and a digital signature.
- Verify that the output PDF has the bookmarks intact and the digital signature removed.
Example Usage:
python pdf_signature_remover.py signed_with_bookmarks.pdf unsigned_with_bookmarks.pdf
This command will process signed_with_bookmarks.pdf, remove the digital signature while keeping the bookmarks, and save the result to unsigned_with_bookmarks.pdf.
Let me know if you have any questions or need further assistance!