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)
| 5471 | # Copy XREF object to another XREF |
| 5472 | # ------------------------------------------------------------------- |
| 5473 | def 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…