pypdf
pypdf copied to clipboard
Removing Form Fields after filling them
After I fill in the form fields, the form fields remain visible and on top of the text filled in causing the filled in text to be hidden under it until you hover.
I tried to remove the annotations, but couldn't achieve that.
What other options do you have to fix this issue?
Environment
Windows 11
$ python -m platform
Windows-11-10.0.22631-SP0
$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.2.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=10.2.0
Code + PDF
This is a minimal, complete example that shows the issue:
import requests
from io import BytesIO
from django.core.files import File
from django.forms.models import model_to_dict
from django.conf.urls.static import static
from pypdf import PdfReader, PdfWriter
import pypdf
ctx = model_to_dict(application)
response = requests.get("http://localhost:8000/static/document/form.pdf")
if not response.ok:
log.error("couldn't get form")
stream = BytesIO(response.content)
reader = PdfReader(stream)
print(reader.get_form_text_fields())
writer = PdfWriter()
writer.append(reader)
for page in writer.pages:
writer.update_page_form_field_values(page, ctx, auto_regenerate=False)
annotations = page.get('/Annots')
if annotations:
new_annotations = [annot for annot in annotations if '/FT' not in annot]
page[pypdf.pdf.Definition('Annots')] = new_annotations
application.arabic_document.save("Some file.pdf", File(writer.write(BytesIO())[1]))
Share here the PDF file(s) that cause the issue. The smaller they are, the better. Let us know if we may add them to our tests!
Traceback
This is the complete traceback I see:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\umansoor\Developer\tarasul\tarasul_admin\models.py", line 220, in _fill_form
new_annotations = [annot for annot in annotations if '/FT' not in annot]
^^^^^^^^^^^^^^^^^^
File "C:\Users\umansoor\Developer\tarasul\venv\Lib\site-packages\pypdf\generic\_base.py", line 309, in __getitem__
return self._get_object_with_check()[key] # type: ignore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
File "C:\Users\umansoor\Developer\tarasul\venv\Lib\site-packages\pypdf\generic\_data_structures.py", line 409, in __getitem__
return dict.__getitem__(self, key).get_object()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 0