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

Method xref_set_key

src/__init__.py:8040–8076  ·  view source on GitHub ↗

Set the value of a PDF dictionary key.

(self, xref, key, value)

Source from the content-addressed store, hash-verified

8038 return text
8039
8040 def xref_set_key(self, xref, key, value):
8041 """Set the value of a PDF dictionary key."""
8042 if self.is_closed:
8043 raise ValueError("document closed")
8044
8045 if not key or not isinstance(key, str) or INVALID_NAME_CHARS.intersection(key) not in (set(), {"/"}):
8046 raise ValueError("bad 'key'")
8047 if not isinstance(value, str) or not value or value[0] == "/" and INVALID_NAME_CHARS.intersection(value[1:]) != set():
8048 raise ValueError("bad 'value'")
8049
8050 pdf = _as_pdf_document(self)
8051 xreflen = mupdf.pdf_xref_len(pdf)
8052 #if not _INRANGE(xref, 1, xreflen-1) and xref != -1:
8053 # THROWMSG("bad xref")
8054 #if len(value) == 0:
8055 # THROWMSG("bad 'value'")
8056 #if len(key) == 0:
8057 # THROWMSG("bad 'key'")
8058 if not _INRANGE(xref, 1, xreflen-1) and xref != -1:
8059 raise ValueError( MSG_BAD_XREF)
8060 if xref != -1:
8061 obj = mupdf.pdf_load_object(pdf, xref)
8062 else:
8063 obj = mupdf.pdf_trailer(pdf)
8064 new_obj = JM_set_object_value(obj, key, value)
8065 if not new_obj.m_internal:
8066 return # did not work: skip update
8067 if xref != -1:
8068 mupdf.pdf_update_object(pdf, xref, new_obj)
8069 else:
8070 n = mupdf.pdf_dict_len(new_obj)
8071 for i in range(n):
8072 mupdf.pdf_dict_put(
8073 obj,
8074 mupdf.pdf_dict_get_key(new_obj, i),
8075 mupdf.pdf_dict_get_val(new_obj, i),
8076 )
8077
8078 def xref_stream(self, xref):
8079 """Get decompressed xref stream."""

Callers 15

embfile_addMethod · 0.95
embfile_updMethod · 0.95
set_markinfoMethod · 0.95
set_pagelayoutMethod · 0.95
set_pagemodeMethod · 0.95
set_colorsMethod · 0.80
scrubMethod · 0.80
set_metadataMethod · 0.80
set_ocMethod · 0.80
set_old_widthsMethod · 0.80
xref_copyMethod · 0.80
set_colorsMethod · 0.80

Calls 4

_as_pdf_documentFunction · 0.85
_INRANGEFunction · 0.85
JM_set_object_valueFunction · 0.85
pdf_trailerMethod · 0.80

Tested by 4

test_rotation2Function · 0.64
test_valid_nameFunction · 0.64
bbox_countFunction · 0.64
test_2736Function · 0.64