- Notifications
You must be signed in to change notification settings - Fork1.5k
Description
Description
When duplicating a page from a PdfReader using copy.deepcopy and adding it to a PdfWriter with writer.add_page, the duplicated page unexpectedly modifies the original page it was duplicated from.
For example, given Page1 with form fields FirstName and LastName initially set to "John" and "Smith":
1.Page2 is created as a duplicate and set to "Boris" and "Norris".
2.In the generated PDF, all instances of FirstName and LastName (including Page1 and Page2) incorrectly contain "Boris" and "Norris", instead of retaining their respective values.
This suggests that the form field references are not properly isolated between the original and duplicated pages.
Environment
$ python -m platformmacOS-14.7.4-arm64-arm-64bit$ python -c"import pypdf;print(pypdf._debug_versions)"pypdf==5.3.0, crypt_provider=('local_crypt_fallback','0.0.0'), PIL=none
Code + PDF
This is a minimal, complete example that shows the issue:
frompypdfimportPdfReader,PdfWriterfromcopyimportdeepcopypdf_path="template.pdf"# Replace with your PDF file pathreader=PdfReader(pdf_path)writer=PdfWriter(clone_from=reader)writer.update_page_form_field_values(writer.pages[0], {"FirstName":"John","LastName":"Smith"})# Add a duplicated pageduplicated_page=writer.add_page(deepcopy(reader.pages[0]))writer.update_page_form_field_values(duplicated_page, {"FirstName":"Boris","LastName":"Norris"})withopen("output.pdf","wb")asoutput_file:writer.write(output_file)
Expected Behavior
Each duplicated page should maintain its own independent form field values:
•Page1 → "John" / "Smith"
•Page2 → "Boris" / "Norris"
Actual Behavior
All instances of FirstName and LastName fields across pages end up displaying "Boris" and "Norris", indicating that form field values are not isolated between duplicated pages.
Attachments
•Template PDF:template.pdf
•Generated PDF with Issue:output.pdf