To modify your program to preserve any bookmarks (also known as outlines) present in the PDF, you'll need to copy the outlines from the PdfReader to the PdfWriter. The pypdf library allows you to access the outlines from the reader and add them to the writer. Here's how you can achieve this:
Access the Outlines from the Reader:
The PdfReader object has an outlines attribute that contains the bookmarks of the PDF.
Copy the Outlines to the Writer:
You need to traverse the outlines recursively to handle nested bookmarks.
Use the add_outline method of PdfWriter to add each bookmark to the new PDF.
Handle Nested Bookmarks:
Bookmarks can be nested, so a recursive approach ensures that all levels of bookmarks are preserved.
Below is the modified program with comments explaining each step:
python
import sys
from pypdf import PdfReader, PdfWriter
from pypdf.generic import Destination
def remove_digital_signature(input_file, output_file):
reader = PdfReader(input_file)
writer = PdfWriter()
# Add all pages to the writer
for page in reader.pages:
writer.add_page(page)
# Remove digital signatures by removing links
writer.remove_links()
# Function to recursively copy bookmarks
def copy_bookmarks(outlines, writer, parent=None):
for outline in outlines:
if isinstance(outline, list):
# Handle nested bookmarks
copy_bookmarks(outline, writer, parent=parent)
elif isinstance(outline, Destination):
# Add a bookmark to the writer
writer.add_outline(outline.title, outline.page, parent=parent)
else:
# Handle other potential bookmark types if necessary
pass
# Copy bookmarks from reader to writer
try:
outlines = reader.outlines
if outlines:
copy_bookmarks(outlines, writer)
except Exception as e:
print(f"Warning: Could not copy bookmarks. Reason: {e}")
# Write the output PDF
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 of Changes:
Recursive copy_bookmarks Function:
This function traverses the outlines (bookmarks) from the reader and adds them to the writer.
It checks if an outline item is a list (indicating nested bookmarks) and recursively processes it.
If the item is a Destination object, it adds it as a bookmark using add_outline.
Error Handling:
Wrapped the bookmark copying process in a try-except block to handle any potential issues gracefully.
If bookmarks cannot be copied for any reason, the program will issue a warning but continue processing.
Preserving Bookmarks:
After adding all pages and removing links, the program copies the bookmarks from the reader to the writer.
This ensures that the resulting PDF retains the original bookmarks.
Usage:
Run the script from the command line with the input and output PDF filenames as arguments:
This will create output.pdf with the digital signatures removed and all original bookmarks preserved.
Dependencies:
Ensure you have the pypdf library installed. If not, you can install it using pip:
bash
pip install pypdf
Notes:
The remove_links() method is used to remove digital signatures by removing all links. Ensure that this is the desired behavior. If you specifically want to remove only digital signatures without affecting other links, you might need a more targeted approach.
This script assumes that bookmarks are properly structured in the source PDF. Complex or malformed bookmarks might require additional handling.
By incorporating these changes, your program will retain the bookmarks from the original PDF while removing digital signatures.