OCG / Layer renaming #796
-
I'm hoping to use PyMuPDF for a script to run through a file with 100+ layers and rename each one depending on a set of criteria or delete that layer. I've read through the docs and can't find the functionality to rename or delete layers. Is this something PyMuPDF can do? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 8 replies
-
Let's check. We will make it work if it does not already 😎 OC is new functionality in PyMuPDF, so I am ready to fill any gaps ... In PDF there are configuration layers, representing ON / OFF states of the various OCGs. Used to switch between different such states, without having to do this for each single OCG. Any such switching is temporary only, i.e. the standard configuration is active again on the next open. When some configuration is active, there is also a way, to set single OCGs on or off temporarily. In addition, for each configuration (including the standard one), you can permanently change the ON / OFF state of OCGs via Then you can combine multiple existing OCGs into logical expressions, so-called OCMDs, OC membership dictionaries. A viewer will evaluate the expression of an OCMD, interpeting false as OFF and true as ON. Hope I haven't forgotten something important ... |
Beta Was this translation helpful? Give feedback.
-
I am aware that desireable functionality is still missing in PyMuPDF:
|
Beta Was this translation helpful? Give feedback.
-
@JustSaX - I have not worked on this in the meantime, so no status change. But I am not sure what you want to achieve: |
Beta Was this translation helpful? Give feedback.
-
Correct, and that has not been addressed as yet. On the other hand, the structure of the relevant |
Beta Was this translation helpful? Give feedback.
-
Can you please let me have an example file which you would like to be cleaned up? |
Beta Was this translation helpful? Give feedback.
-
Looked at your file - thanks for providing it. doc=fitz.open("OCG_layer_example.pdf")
print(doc.xref_object(doc.pdf_catalog()))
<<
/Type /Catalog
/Pages 1 0 R
/OCProperties <<
/OCGs [ 18 0 R 23 0 R 28 0 R ]
/D <<
/ON [ 18 0 R 23 0 R 28 0 R ]
/OFF [ ]
/Order [ 18 0 R 23 0 R 28 0 R ]
/RBGroups [ ]
>>
>>
>>
pprint(doc.get_ocgs())
{18: {'intent': ['View'], 'name': 'Layer1', 'on': True, 'usage': 'Artwork'},
23: {'intent': ['View'], 'name': 'Layer2', 'on': True, 'usage': 'Artwork'},
28: {'intent': ['View'], 'name': 'Layer3', 'on': True, 'usage': 'Artwork'}} Each of the 3 OCGs is used by annotations which highlight some text - apparently using a specific color each. doc.set_layer(-1, on=[18, 23], off=[28])
# this will change the catalog like this:
print(doc.xref_object(doc.pdf_catalog()))
<<
/Type /Catalog
/Pages 1 0 R
/OCProperties <<
/OCGs [ 18 0 R 23 0 R 28 0 R ]
/D <<
/RBGroups [ ]
/ON [ 18 0 R 23 0 R ]
/Order [ 18 0 R 23 0 R 28 0 R ]
/OFF [ 28 0 R ]
>>
>>
>> In Adobe and other viewers, Layer3 is still there but set to OFF. # after open the file in original state:
occp = doc.xref_get_key(doc.pdf_catalog(), "OCProperties")
print(occp)
('dict', '<</OCGs[18 0 R 23 0 R 28 0 R]/D<</ON[18 0 R 23 0 R 28 0 R]/OFF[]/Order[18 0 R 23 0 R 28 0 R]/RBGroups[]>>>>')
# remove ref to xref 28 and replace the occp
new_occp = occp[1].replace("28 0 R", "")
doc.xref_set_key(doc.pdf_catalog(), "OCProperties", new_occp)
# done |
Beta Was this translation helpful? Give feedback.
-
Is the following still the way to go about delete an OCG?
|
Beta Was this translation helpful? Give feedback.
I am aware that desireable functionality is still missing in PyMuPDF:
doc.add_ocg(name, config=- 1, on=True, intent='View', usage='Artwork')
, there is no way yet to update an existing OCG. Especially the OCG name might be interesting, because it is used in viewers as an identifier. Its other properties probably are less interesting (except maybeintent
)./Order
entry of a layer. This entry decides if and how OCGs are to be presented by viewers. What I am doing now is simply appending any new OCG to that array, so it will appear in a viewer.