Skip to content

Cannot show optional content group: AttributeError: module 'fitz.mupdf' has no attribute 'pdf_array_push_drop' #3180

Closed
@stefaneidelloth

Description

@stefaneidelloth

Description of the bug

I tried to set the visibility of an optional content group with

doc.set_layer(config=-1, on=[first_id])

and got

AttributeError: module 'fitz.mupdf' has no attribute 'pdf_array_push_drop'

=> How to correctly show/hide optional content groups?

How to reproduce the bug

Full example:

import fitz  # PyMuPDF

doc = fitz.open()
page = doc.new_page()

# Define the items for the combo box
combo_items = ['first', 'second', 'third']

# Create a combo box field
combo_box = fitz.Widget()  # create a new widget
combo_box.field_type = fitz.PDF_WIDGET_TYPE_COMBOBOX
combo_box.field_name = "myComboBox"
combo_box.field_value = combo_items[0]
combo_box.choice_values = combo_items
combo_box.rect = fitz.Rect(50, 50, 200, 75)  # position of the combo box
combo_box.script_change = """
var value = event.value;
app.alert('You selected: ' + value);

//todo: show selected image/optional content group/layer

"""

# Insert the combo box into the page
# https://pymupdf.readthedocs.io/en/latest/page.html#Page.add_widget
page.add_widget(combo_box)

# Create optional content groups
# https://github.com/pymupdf/PyMuPDF-Utilities/blob/master/jupyter-notebooks/optional-content.ipynb


# Load images and create OCGs for each
optional_content_group_ids = {}
for i, item in enumerate(combo_items):
    optional_content_group_id = doc.add_ocg(item, on=False)
    optional_content_group_ids[item] = optional_content_group_id
    rect = fitz.Rect(50, 100, 250, 300)
    image_file_name = f'{item}.png'
    # xref = page.insert_image(
    #    rect,
    #    filename=image_file_name,
    #    oc=optional_content_group_id,
    # )


first_id = optional_content_group_ids['first']
second_id = optional_content_group_ids['second']
third_id = optional_content_group_ids['third']

# https://pymupdf.readthedocs.io/en/latest/document.html#Document.set_layer


doc.set_layer(-1, basestate="OFF")
layers = doc.get_layer()
doc.set_layer(config=-1, on=[first_id])

# https://pymupdf.readthedocs.io/en/latest/document.html#Document.set_layer_ui_config
# configs = doc.layer_ui_configs()
# doc.set_layer_ui_config(0, fitz.PDF_OC_ON)
# doc.set_layer_ui_config('third', action=2)

# Save the PDF
doc.save("output.pdf")
doc.close()

Example images first.png, second.png, third.png

first
second
third

PyMuPDF version

1.23.23

Operating system

Windows

Python version

3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions