Skip to content
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

Setting RadioButton values to 'Off' doesn't set them to off after the file is saved #3314

Closed
ag-gaphp opened this issue Mar 27, 2024 · 4 comments

Comments

@ag-gaphp
Copy link

ag-gaphp commented Mar 27, 2024

Moving my reply from #2333 because this seems like a regression according to #711.

I have 2 in a document, and I can change almost any other property of the radio buttons without issue and see those changes reflected after a doc.save(path, garbage=1). The only change I am unable to affect is the value of the radios.

System info:

  • PyMuPDF v1.23.26, v1.24.0
  • Python v3.12.2
  • Windows 10 Pro
  • Using a venv in VSCodium

I have 2 radio buttons in a group, the only 2 on the page. Here is their info according to PyMuPDF:

>>> fdoc = fitz.open(doc_path)
>>> page = fdoc.load_page(0)
>>> btn1 = page.load_widget(66)
>>> btn2 = page.load_widget(69)
>>> btn1.button_states()
{'normal': ['Off', '1'], 'down': None}
>>> btn2.button_states()
{'normal': ['Off', '2'], 'down': None}
>>> print(btn1.field_value, ", ", btn2.field_value)
Off, 2

This is how they read when I first export from LibreOffice Draw. Then I do the following:

>>> btn2.field_value = 'Off'
>>> btn2.update()
>>> btn2.field_value
'Off'

It appears to be off. However, when I save the file and open in any PDF viewer, btn2 is always selected. This is whether I set both values to Off, False, True, anything. No matter the combination I've tried to turn both of them off, or at least check btn1 instead of btn2, it never actually changes in the saved PDF even though PyMuPDF is reporting that the value has changed just before saving.

Attaching the PDF in question as it has been exported from LibreOffice, before I run my PyMuPDF script on it:
ROI - General.pdf

PDF viewers I've tested with: Firefox, Chrome, Edge, LaTeX/PDF viewer extenson in VSCodium, Adobe Acrobat Pro, Okular

Originally posted by @ag-gaphp in #2333 (comment)

@ag-gaphp
Copy link
Author

I have also confirmed similar behavior in PyMuPDF v1.24.0

The only difference now is that both of my RadioButtons are checked, even after I set them both to 'Off'.

@JorjMcKie
Copy link
Collaborator

This works: the two buttons are being toggled 2 times and then show the correct values.

import fitz

doc = fitz.open("ROI-General.pdf")
page = doc[0]
btn1 = page.load_widget(66)
btn2 = page.load_widget(69)
print(f"btn1={btn1.field_value}, btn2={btn2.field_value} - {doc.name=}")

btn1.field_value = False
btn1.update()

btn2.field_value = True
btn2.update()

doc.save("x.pdf")
doc = fitz.open("x.pdf")
page = doc[0]
btn1 = page.load_widget(66)
btn2 = page.load_widget(69)
print(f"btn1={btn1.field_value}, btn2={btn2.field_value} - {doc.name=}")
btn1.field_value = True
btn1.update()

btn2.field_value = False
btn2.update()
doc.save("y.pdf")
doc = fitz.open("y.pdf")
page = doc[0]
btn1 = page.load_widget(66)
btn2 = page.load_widget(69)
print(f"btn1={btn1.field_value}, btn2={btn2.field_value} - {doc.name=}")

Original:
image
x.pdf:
image
y.pdf:
image

@ag-gaphp
Copy link
Author

Sorry for having the two spots of discussion, I replied in #2333 (reply in thread)

@ag-gaphp
Copy link
Author

False alarm, my bad. I may have been working with a corrupted file at some point, but the takeaway is to specifically use True/False for a Radio/Checkbox when you want to turn it on/off and the backend should handle everything else appropriately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants