-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
working with checkbox with /Kids or extrange /V #961
Comments
Thank you for your bug report! Would you mind sharing your PyPDF2 version + the environment you're using? (It's part of the bug ticket template)
What does that mean? There is no way to do what? |
@Luisonson Have you seen https://pypdf2.readthedocs.io/en/latest/user/forms.html#filling-out-forms ? Does that help? If not, why? |
Hello, Thanks for your answer. I can't select/click the checkboxes or deselect. Also, some checkboxes appears just as /kids of another checkbox, so I can't interact with it as shown in the example with the checkbox BOTON_JORN that has 4 /kids... and those kids are another 4 checkboxes that the only thing I know about them is that are IndirectObject(X, 0).
Yes, part of the code I have pasted is from there, but does not work in this PDF with the checkboxes. |
Another hint: If I try to update the text boxes, is ok, BUT, if i try to update the checkboxes (unsusesfully), then the text of the boxes is not shown unless I select the box: Updating two text boxes from PyPDF2 import PdfFileReader, PdfFileWriter
reader = PdfReader("filled-out_5.pdf")
writer = PdfWriter()
page = reader.pages[0]
fields3 = reader.get_fields()
writer.add_page(page)
writer.update_page_form_field_values(
writer.getPage(0), {"Texto41": "Test38",
"Texto56": "Test2"}
)
with open("filled-out_5_out.pdf", "wb") as output_stream:
writer.write(output_stream)
reader.stream.close() Updating two textboxes and trying to update one checkbox (the bug of the text not showing appears) from PyPDF2 import PdfFileReader, PdfFileWriter
reader = PdfReader("filled-out_5.pdf")
writer = PdfWriter()
page = reader.pages[0]
fields3 = reader.get_fields()
writer.add_page(page)
writer.update_page_form_field_values(
writer.getPage(0), {"Texto41": "Test38",
"Texto56": "Test2"}
)
writer.update_page_form_field_values(
writer.getPage(0), {"BOTON_TPCON1": "/540"}
)
# write "output" to PyPDF2-output.pdf
with open("filled-out_5_out.pdf", "wb") as output_stream:
writer.write(output_stream)
reader.stream.close() Also, another error. After the new file is saved, If you try to obtain the fields of the new file with: reader = PdfReader("filled-out_5_out.pdf")
reader.get_fields() Does not show any field. I have to open the pdf with Adobe and save it with the adobe, then the code below works |
No, previusly I was using pypdf2 1.26 and i had the code to mitigate that issue (def set_need_appearances_writer(writer: PdfFileWriter)) on my first message. But with pypdf2 2.1.0 that function is not needed... until you try to modify a checkbox as I just told you in the previous message :( |
Oh, so it is a regression? It was working with 1.26 and now it is not working anymore with 2.1.0? I'll have a closer look today evening after work :-) |
I'm sorry, maybe I'm messing up things. |
I'll post a series of comments here to keep track / let people know how I investigate the issue.
That gives uncompressed-1.pdf |
Next I used PyPDF2 to find the form fields and their names. I looked for Before filling it:
After:
I notice two differences:
|
@Luisonson This ticks one checkbox: from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import NameObject
from typing import Dict
def update_checkbox_values(page, fields: Dict[str, bool]):
for j in range(0, len(page['/Annots'])):
writer_annot = page['/Annots'][j].getObject()
field_name = writer_annot.get('/T')
if field_name in fields:
print(f"Found {field_name}")
assert writer_annot.get('/FT') == '/Btn'
print(writer_annot)
if fields[field_name]:
print("\tCheck it")
writer_annot.update({
NameObject("/V"): NameObject("/S#ed"),
NameObject("/AS"): NameObject("/S#ed"),
})
for key in writer_annot:
print((key, writer_annot[key]))
else:
writer_annot.update({
NameObject("/V"): NameObject("/No"),
NameObject("/AS"): NameObject("/Off")
})
reader = PdfReader("TEMPORAL.COMPLETO12.de.mayo_unlocked.pdf")
# See which fields exist
fields = reader.get_form_text_fields()
print(fields)
writer = PdfWriter()
writer.set_need_appearances_writer()
writer.add_page(reader.pages[0])
update_checkbox_values(writer.pages[0], {"TEXTOCasilla de verificación25": False})
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream) Does this help? |
Good Morning, reader = PdfReader("filled-out_5.pdf")
# See which fields exist
fields = reader.getFields()
print(fields) OUTPUT: I need to modify BOTON_TPCON1, from /450401 to /540. But, with your example: so.... On the other hand, yesterday someone told me about the fdf file, whitch is an ascii template (easy to modify), whitch you open and merge with the pdf and the pdf will pick up the values of the fdf file. Is pyPDF2 capable of handling fdf files? If not, would be a nice feature to add. |
I've seen fdf being mentioned somewhere, but I have no experience with it. I'm open to PRs, but I also need to check if adding fdf support is in scope for PyPDF2. |
For example, in my case, for change some values of the first page is:
As you can see, it is quite simple and self-explicatory. BUT, pyPDF2 has to be capable of update any value. As an another example: To generate an fdf file, open the pdf file with acrobat -> file -> create -> create form |
@MartinThoma |
I (sadly) have to agree: I don't see FDF support happening soon and I don't see us getting process here. I have added a link to #1181 . Feel free to add here or there more details on FDF (PRs introducing support would also be very welcome!). The fact that I'm closing this is a reflection on the fact that no core contributor will pick this up in the next half year. We want this support in pypdf, but we don't have the resources to make it happen any time soon. |
OK, no problem. Thanks for your time. |
I'm trying to automate filling this PDF:
TEMPORAL COMPLETO12 de mayo_unlocked.pdf
I have no problem with the text, but with the checkboxes there is no way. Many /Btn have /Kids those /kids are other checkboxes that appear as "indirectObject". Also, normal checkboxes I can't select/modify in this pdf (examples bellow)
Code
This example was written for the pypdf2 1.26.0 version
If I modified the pdf manually and read the fields...:
Another checkbox, with NO /kids but I can't select/modify is:
'TEXTOCasilla de verificación25' when selected has the value '/S#ED'
Thanks for your time.
PDF
TEMPORAL COMPLETO12 de mayo_unlocked.pdf
The text was updated successfully, but these errors were encountered: