Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

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

Closed
stefaneidelloth opened this issue Feb 19, 2024 · 5 comments

Comments

@stefaneidelloth
Copy link

stefaneidelloth commented Feb 19, 2024

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

@stefaneidelloth stefaneidelloth changed the title AttributeError: module 'fitz.mupdf' has no attribute 'pdf_array_push_drop' Cannot show optional content group: AttributeError: module 'fitz.mupdf' has no attribute 'pdf_array_push_drop' Feb 19, 2024
julian-smith-artifex-com added a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Feb 19, 2024
We need to use mupdf.pdf_array_push(), not mupdf.pdf_array_push_drop().

Addresses pymupdf#3180.
@julian-smith-artifex-com
Copy link
Collaborator

Thanks for reporting this. I have a fix in my tree.

@stefaneidelloth
Copy link
Author

Thank you. I think this does not make the ocm visible, yet.

I would expect something like

doc.show_ocgs([first_id])

or

doc.set_ocg(first_id, on=True)

But there are no such methods.

=> How to programmatically show a previously hidden ocg (or layer)?

@JorjMcKie
Copy link
Collaborator

@stefaneidelloth Please have a look at layer_ui_configs() and set_layer_ui_config(number, action=0).
They get / set the temporary visibility - like a supporting PDF viewer would via its UI.

Close by in that part of the documentation, you will also find ways to make permanent changes.

@stefaneidelloth
Copy link
Author

stefaneidelloth commented Feb 19, 2024

Thanks. As you see in the comments /end of the example, I already tried that. What would be a modified version of the example that works? (I also attached example image files just now.)

julian-smith-artifex-com added a commit that referenced this issue Feb 19, 2024
We need to use mupdf.pdf_array_push(), not mupdf.pdf_array_push_drop().

Addresses #3180.
julian-smith-artifex-com added a commit that referenced this issue Feb 19, 2024
We need to use mupdf.pdf_array_push(), not mupdf.pdf_array_push_drop().

Addresses #3180.
@julian-smith-artifex-com
Copy link
Collaborator

The pdf_array_push_drop bug is fixed in 1.23.24. I'll leave this issue open though until the other questions are understood/answered.

@pymupdf pymupdf locked and limited conversation to collaborators Feb 21, 2024
@julian-smith-artifex-com julian-smith-artifex-com converted this issue into discussion #3195 Feb 21, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants