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

Pushbutton widget appears as check box #2208

Closed
qwkfinger opened this issue Feb 2, 2023 · 3 comments
Closed

Pushbutton widget appears as check box #2208

qwkfinger opened this issue Feb 2, 2023 · 3 comments
Assignees

Comments

@qwkfinger
Copy link

Describe the bug (mandatory)

After creating a pushbutton widget, it shows as a checkbox in the final pdf file.

To Reproduce (mandatory)

import fitz

doc = fitz.open()
doc.new_page(0)
page = doc[0]

widget = fitz.Widget()
widget.field_type = fitz.PDF_WIDGET_TYPE_BUTTON
widget.field_flags = fitz.PDF_BTN_FIELD_IS_PUSHBUTTON
widget.rect = fitz.Rect(5, 5, 20, 20)
widget.field_name = "Test"
widget.field_value = "Test button"
widget.fill_color = (0, 0, 1)
annot = page.add_widget(widget)
for w in page.widgets():
    print(w) # prints 'CheckBox' widget on page 0 of <new PDF, doc# 1>
doc.save("button_test.pdf")

Expected behavior (optional)

The widget should show as a button. This worked in older versions (tested with version 1.16.18) and shows the same result in 1.18.4.

Your configuration (mandatory)

  • 3.11.0 | packaged by conda-forge | (main, Oct 25 2022, 06:12:32) [MSC v.1929 64 bit (AMD64)]
    win32
  • PyMuPDF 1.21.1: Python bindings for the MuPDF 1.21.1 library.
  • Version date: 2022-12-13 00:00:01.
  • Built for Python 3.11 on win32 (64-bit).
  • Windows 10 64bit
  • mambaforge
  • python 3.7, 3.8, 3.10, 3.11 (all tested, same result)
  • pymupdf 1.21.1, installed with wheel and pip

Additional context (optional)

If this is truly a bug, maybe sombeody knows a workaround.

@JorjMcKie
Copy link
Collaborator

It is a bug, and it will be fixed in the next version.
Some background:
PDF only knows field type /Btn in the field dictionary object. Differentiation between subtypes like radio button and checkbox only occur via setting different bit positions of the field flags key /Ff. See here:
grafik
The bug happens by incorrectly setting the respective bit.
As a workaround try the following:

  1. create the button
  2. the determine ths xref of the new field
  3. "manually" force the field flags to assume the right value:
doc.xref_set_key(field.xref, "Ff", "32768")  # represents value 1 << 15 (radio button)

@qwkfinger
Copy link
Author

Thank you,

the workaround changes the widget to a radiobutton, however, the following creates a button:

doc.xref_set_key(annot.xref, "Ff", "65536")

But the button has no caption.

@JorjMcKie
Copy link
Collaborator

Thank you,

the workaround changes the widget to a radiobutton, however, the following creates a button:

doc.xref_set_key(annot.xref, "Ff", "65536")

But the button has no caption.

The workaround for a pushbutton would need additional lowlevel code:

caption = fitz.get_pdf_str("push me")
doc.xref_set_key(xref, "MK/CA", caption)

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

3 participants