MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / test_3180

Function test_3180

tests/test_optional_content.py:77–139  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

75
76
77def test_3180():
78 doc = pymupdf.open()
79 page = doc.new_page()
80
81 # Define the items for the combo box
82 combo_items = ['first', 'second', 'third']
83
84 # Create a combo box field
85 combo_box = pymupdf.Widget() # create a new widget
86 combo_box.field_type = pymupdf.PDF_WIDGET_TYPE_COMBOBOX
87 combo_box.field_name = "myComboBox"
88 combo_box.field_value = combo_items[0]
89 combo_box.choice_values = combo_items
90 combo_box.rect = pymupdf.Rect(50, 50, 200, 75) # position of the combo box
91 combo_box.script_change = """
92 var value = event.value;
93 app.alert('You selected: ' + value);
94
95 //var group_id = optional_content_group_ids[value];
96
97 """
98
99 # Insert the combo box into the page
100 # https://pymupdf.readthedocs.io/en/latest/page.html#Page.add_widget
101 page.add_widget(combo_box)
102
103 # Create optional content groups
104 # https://github.com/pymupdf/PyMuPDF-Utilities/blob/master/jupyter-notebooks/optional-content.ipynb
105
106
107 # Load images and create OCGs for each
108 optional_content_group_ids = {}
109 for i, item in enumerate(combo_items):
110 optional_content_group_id = doc.add_ocg(item, on=False)
111 optional_content_group_ids[item] = optional_content_group_id
112 rect = pymupdf.Rect(50, 100, 250, 300)
113 image_file_name = f'{item}.png'
114 # xref = page.insert_image(
115 # rect,
116 # filename=image_file_name,
117 # oc=optional_content_group_id,
118 # )
119
120
121 first_id = optional_content_group_ids['first']
122 second_id = optional_content_group_ids['second']
123 third_id = optional_content_group_ids['third']
124
125 # https://pymupdf.readthedocs.io/en/latest/document.html#Document.set_layer
126
127
128 doc.set_layer(-1, basestate="OFF")
129 layers = doc.get_layer()
130 doc.set_layer(config=-1, on=[first_id])
131
132 # https://pymupdf.readthedocs.io/en/latest/document.html#Document.set_layer_ui_config
133 # configs = doc.layer_ui_configs()
134 # doc.set_layer_ui_config(0, pymupdf.PDF_OC_ON)

Callers

nothing calls this directly

Calls 7

new_pageMethod · 0.80
add_widgetMethod · 0.80
add_ocgMethod · 0.80
set_layerMethod · 0.80
get_layerMethod · 0.80
saveMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…