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

Function xref_copy

src_classic/utils.py:5473–5507  ·  view source on GitHub ↗

Copy a PDF dictionary object to another one given their xref numbers. Args: doc: PDF document object source: source xref number target: target xref number, the xref must already exist keep: an optional list of 1st level keys in target that should not be

(doc: Document, source: int, target: int, *, keep: list = None)

Source from the content-addressed store, hash-verified

5471# Copy XREF object to another XREF
5472# -------------------------------------------------------------------
5473def xref_copy(doc: Document, source: int, target: int, *, keep: list = None) -> None:
5474 """Copy a PDF dictionary object to another one given their xref numbers.
5475
5476 Args:
5477 doc: PDF document object
5478 source: source xref number
5479 target: target xref number, the xref must already exist
5480 keep: an optional list of 1st level keys in target that should not be
5481 removed before copying.
5482 Notes:
5483 This works similar to the copy() method of dictionaries in Python. The
5484 source may be a stream object.
5485 """
5486 if doc.xref_is_stream(source):
5487 # read new xref stream, maintaining compression
5488 stream = doc.xref_stream_raw(source)
5489 doc.update_stream(
5490 target,
5491 stream,
5492 compress=False, # keeps source compression
5493 new=True, # in case target is no stream
5494 )
5495
5496 # empty the target completely, observe exceptions
5497 if keep is None:
5498 keep = []
5499 for key in doc.xref_get_keys(target):
5500 if key in keep:
5501 continue
5502 doc.xref_set_key(target, key, "null")
5503 # copy over all source dict items
5504 for key in doc.xref_get_keys(source):
5505 item = doc.xref_get_key(source, key)
5506 doc.xref_set_key(target, key, item[1])
5507 return None

Callers

nothing calls this directly

Calls 6

xref_is_streamMethod · 0.80
xref_stream_rawMethod · 0.80
update_streamMethod · 0.80
xref_get_keysMethod · 0.80
xref_set_keyMethod · 0.80
xref_get_keyMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…