MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / add_ocg

Method add_ocg

src/__init__.py:4151–4219  ·  view source on GitHub ↗

Add new optional content group.

(self, name, config=-1, on=1, intent=None, usage=None)

Source from the content-addressed store, hash-verified

4149 mupdf.ll_pdf_read_ocg( pdf.m_internal)
4150
4151 def add_ocg(self, name, config=-1, on=1, intent=None, usage=None):
4152 """Add new optional content group."""
4153 xref = 0
4154 pdf = _as_pdf_document(self)
4155
4156 # make the OCG
4157 ocg = mupdf.pdf_add_new_dict(pdf, 3)
4158 mupdf.pdf_dict_put(ocg, PDF_NAME('Type'), PDF_NAME('OCG'))
4159 mupdf.pdf_dict_put_text_string(ocg, PDF_NAME('Name'), name)
4160 intents = mupdf.pdf_dict_put_array(ocg, PDF_NAME('Intent'), 2)
4161 if not intent:
4162 mupdf.pdf_array_push(intents, PDF_NAME('View'))
4163 elif not isinstance(intent, str):
4164 assert 0, f'fixme: intent is not a str. {type(intent)=} {type=}'
4165 #n = len(intent)
4166 #for i in range(n):
4167 # item = intent[i]
4168 # c = JM_StrAsChar(item);
4169 # if (c) {
4170 # pdf_array_push(gctx, intents, pdf_new_name(gctx, c));
4171 # }
4172 # Py_DECREF(item);
4173 #}
4174 else:
4175 mupdf.pdf_array_push(intents, mupdf.pdf_new_name(intent))
4176 use_for = mupdf.pdf_dict_put_dict(ocg, PDF_NAME('Usage'), 3)
4177 ci_name = mupdf.pdf_new_name("CreatorInfo")
4178 cre_info = mupdf.pdf_dict_put_dict(use_for, ci_name, 2)
4179 mupdf.pdf_dict_put_text_string(cre_info, PDF_NAME('Creator'), "PyMuPDF")
4180 if usage:
4181 mupdf.pdf_dict_put_name(cre_info, PDF_NAME('Subtype'), usage)
4182 else:
4183 mupdf.pdf_dict_put_name(cre_info, PDF_NAME('Subtype'), "Artwork")
4184 indocg = mupdf.pdf_add_object(pdf, ocg)
4185
4186 # Insert OCG in the right config
4187 ocp = JM_ensure_ocproperties(pdf)
4188 obj = mupdf.pdf_dict_get(ocp, PDF_NAME('OCGs'))
4189 mupdf.pdf_array_push(obj, indocg)
4190
4191 if config > -1:
4192 obj = mupdf.pdf_dict_get(ocp, PDF_NAME('Configs'))
4193 if not mupdf.pdf_is_array(obj):
4194 raise ValueError( MSG_BAD_OC_CONFIG)
4195 cfg = mupdf.pdf_array_get(obj, config)
4196 if not cfg.m_internal:
4197 raise ValueError( MSG_BAD_OC_CONFIG)
4198 else:
4199 cfg = mupdf.pdf_dict_get(ocp, PDF_NAME('D'))
4200
4201 obj = mupdf.pdf_dict_get(cfg, PDF_NAME('Order'))
4202 if not obj.m_internal:
4203 obj = mupdf.pdf_dict_put_array(cfg, PDF_NAME('Order'), 1)
4204 mupdf.pdf_array_push(obj, indocg)
4205 if on:
4206 obj = mupdf.pdf_dict_get(cfg, PDF_NAME('ON'))
4207 if not obj.m_internal:
4208 obj = mupdf.pdf_dict_put_array(cfg, PDF_NAME('ON'), 1)

Callers 5

test_textbox2Function · 0.80
test_textbox4Function · 0.80
test_oc1Function · 0.80
test_oc2Function · 0.80
test_3180Function · 0.80

Calls 3

_as_pdf_documentFunction · 0.85
PDF_NAMEFunction · 0.85
JM_ensure_ocpropertiesFunction · 0.85

Tested by 5

test_textbox2Function · 0.64
test_textbox4Function · 0.64
test_oc1Function · 0.64
test_oc2Function · 0.64
test_3180Function · 0.64