pymupdf / PyMuPDF

PyMuPDF is a high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
https://pymupdf.readthedocs.io
GNU Affero General Public License v3.0
4.54k stars 447 forks source link

Unable to create a checked radiobutton #3481

Closed dantard closed 1 month ago

dantard commented 1 month ago

Description of the bug

I'm unable to create a RADIOBUTTON which is already checked:

import fitz
from pymupdf import Widget, Rect
from pymupdf.mupdf import PDF_WIDGET_TYPE_RADIOBUTTON
doc = fitz.open()
page = doc.new_page()

widget = Widget()
widget.field_name = "radio"
widget.rect = Rect(100, 100, 120, 120)
widget.field_type = PDF_WIDGET_TYPE_RADIOBUTTON
widget.field_value = True 
page.add_widget(widget)

#page = doc[0]
#widgets = page.widgets()

#for w in widgets:
#    w.field_value = True
#    w.update()

doc.save("radio_button_pdf.pdf")
doc.close()

gives the error:

Traceback (most recent call last):
  File "/home/danilo/work/swikv3/radiob.py", line 14, in <module>
    page.add_widget(widget)
  File "/home/danilo/.local/lib/python3.10/site-packages/pymupdf/__init__.py", line 8477, in add_widget
    widget.update()
  File "/home/danilo/.local/lib/python3.10/site-packages/pymupdf/__init__.py", line 7151, in update
    self._validate()
  File "/home/danilo/.local/lib/python3.10/site-packages/pymupdf/__init__.py", line 7065, in _validate
    self._checker()  # any field_type specific checks
  File "/home/danilo/.local/lib/python3.10/site-packages/pymupdf/__init__.py", line 6956, in _checker
    kids_type, kids_value = doc.xref_get_key(self.xref, "Parent/Kids")
  File "/home/danilo/.local/lib/python3.10/site-packages/pymupdf/__init__.py", line 5789, in xref_get_key
    raise ValueError( MSG_BAD_XREF)
ValueError: bad xref

It does not happen if field_value is set to False.

I'm however able to update the value afterwards (commented code). Not sure if it is a bug or a "feature".

How to reproduce the bug

Just by running the code

PyMuPDF version

1.24.3

Operating system

Linux

Python version

3.10

JorjMcKie commented 1 month ago

This is a non-implemented feature. Radio buttons are intended to be set on or off in conjunction with others in the same group. PyMuPDF does not currently support RB groups - this is the reason for this error.