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

Function JM_set_object_value

src/__init__.py:21557–21602  ·  view source on GitHub ↗

Set a PDF dict key to some value

(obj, key, value)

Source from the content-addressed store, hash-verified

21555
21556
21557def JM_set_object_value(obj, key, value):
21558 '''
21559 Set a PDF dict key to some value
21560 '''
21561 eyecatcher = "fitz: replace me!"
21562 pdf = mupdf.pdf_get_bound_document(obj)
21563 # split PDF key at path seps and take last key part
21564 list_ = key.split('/')
21565 len_ = len(list_)
21566 i = len_ - 1
21567 skey = list_[i]
21568
21569 del list_[i] # del the last sub-key
21570 len_ = len(list_) # remaining length
21571 testkey = mupdf.pdf_dict_getp(obj, key) # check if key already exists
21572 if not testkey.m_internal:
21573 #No, it will be created here. But we cannot allow this happening if
21574 #indirect objects are referenced. So we check all higher level
21575 #sub-paths for indirect references.
21576 while len_ > 0:
21577 t = '/'.join(list_) # next high level
21578 if mupdf.pdf_is_indirect(mupdf.pdf_dict_getp(obj, JM_StrAsChar(t))):
21579 raise Exception(f"path to '{JM_StrAsChar(skey)}' has indirects")
21580 del list_[len_ - 1] # del last sub-key
21581 len_ = len(list_) # remaining length
21582 # Insert our eyecatcher. Will create all sub-paths in the chain, or
21583 # respectively remove old value of key-path.
21584 mupdf.pdf_dict_putp(obj, key, mupdf.pdf_new_text_string(eyecatcher))
21585 testkey = mupdf.pdf_dict_getp(obj, key)
21586 if not mupdf.pdf_is_string(testkey):
21587 raise Exception(f"cannot insert value for '{key}'")
21588 temp = mupdf.pdf_to_text_string(testkey)
21589 if temp != eyecatcher:
21590 raise Exception(f"cannot insert value for '{key}'")
21591 # read the result as a string
21592 res = JM_object_to_buffer(obj, 1, 0)
21593 objstr = JM_EscapeStrFromBuffer(res)
21594
21595 # replace 'eyecatcher' by desired 'value'
21596 nullval = f"/{skey}({eyecatcher})"
21597 newval = f"/{skey} {value}"
21598 newstr = objstr.replace(nullval, newval, 1)
21599
21600 # make PDF object from resulting string
21601 new_obj = JM_pdf_obj_from_str(pdf, newstr)
21602 return new_obj
21603
21604
21605def JM_set_ocg_arrays(conf, basestate, on, off, rbgroups, locked):

Callers 1

xref_set_keyMethod · 0.85

Calls 4

JM_StrAsCharFunction · 0.85
JM_object_to_bufferFunction · 0.85
JM_EscapeStrFromBufferFunction · 0.85
JM_pdf_obj_from_strFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…