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

Method set_layer

src/__init__.py:6790–6853  ·  view source on GitHub ↗

Set the PDF keys /ON, /OFF, /RBGroups of an OC layer.

(self, config, basestate=None, on=None, off=None, rbgroups=None, locked=None)

Source from the content-addressed store, hash-verified

6788 return True
6789
6790 def set_layer(self, config, basestate=None, on=None, off=None, rbgroups=None, locked=None):
6791 """Set the PDF keys /ON, /OFF, /RBGroups of an OC layer."""
6792 if self.is_closed:
6793 raise ValueError("document closed")
6794 ocgs = set(self.get_ocgs().keys())
6795 if ocgs == set():
6796 raise ValueError("document has no optional content")
6797
6798 if on:
6799 if type(on) not in (list, tuple):
6800 raise ValueError("bad type: 'on'")
6801 s = set(on).difference(ocgs)
6802 if s != set():
6803 raise ValueError(f"bad OCGs in 'on': {s}")
6804
6805 if off:
6806 if type(off) not in (list, tuple):
6807 raise ValueError("bad type: 'off'")
6808 s = set(off).difference(ocgs)
6809 if s != set():
6810 raise ValueError(f"bad OCGs in 'off': {s}")
6811
6812 if locked:
6813 if type(locked) not in (list, tuple):
6814 raise ValueError("bad type: 'locked'")
6815 s = set(locked).difference(ocgs)
6816 if s != set():
6817 raise ValueError(f"bad OCGs in 'locked': {s}")
6818
6819 if rbgroups:
6820 if type(rbgroups) not in (list, tuple):
6821 raise ValueError("bad type: 'rbgroups'")
6822 for x in rbgroups:
6823 if not type(x) in (list, tuple):
6824 raise ValueError(f"bad RBGroup '{x}'")
6825 s = set(x).difference(ocgs)
6826 if s != set():
6827 raise ValueError(f"bad OCGs in RBGroup: {s}")
6828
6829 if basestate:
6830 basestate = str(basestate).upper()
6831 if basestate == "UNCHANGED":
6832 basestate = "Unchanged"
6833 if basestate not in ("ON", "OFF", "Unchanged"):
6834 raise ValueError("bad 'basestate'")
6835 pdf = _as_pdf_document(self)
6836 ocp = mupdf.pdf_dict_getl(
6837 mupdf.pdf_trailer( pdf),
6838 PDF_NAME('Root'),
6839 PDF_NAME('OCProperties'),
6840 )
6841 if not ocp.m_internal:
6842 return
6843 if config == -1:
6844 obj = mupdf.pdf_dict_get( ocp, PDF_NAME('D'))
6845 else:
6846 obj = mupdf.pdf_array_get(
6847 mupdf.pdf_dict_get( ocp, PDF_NAME('Configs')),

Callers 2

test_oc1Function · 0.80
test_3180Function · 0.80

Calls 5

get_ocgsMethod · 0.95
_as_pdf_documentFunction · 0.85
PDF_NAMEFunction · 0.85
JM_set_ocg_arraysFunction · 0.85
pdf_trailerMethod · 0.80

Tested by 2

test_oc1Function · 0.64
test_3180Function · 0.64