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 sho
(doc: 'Document', source: int, target: int, *, keep: list = None)
| 7876 | return self.parent.page_xref(self.number) |
| 7877 | |
| 7878 | def xref_copy(doc: 'Document', source: int, target: int, *, keep: list = None) -> None: |
| 7879 | """Copy a PDF dictionary object to another one given their xref numbers. |
| 7880 | |
| 7881 | Args: |
| 7882 | doc: PDF document object |
| 7883 | source: source xref number |
| 7884 | target: target xref number, the xref must already exist |
| 7885 | keep: an optional list of 1st level keys in target that should not be |
| 7886 | removed before copying. |
| 7887 | Notes: |
| 7888 | This works similar to the copy() method of dictionaries in Python. The |
| 7889 | source may be a stream object. |
| 7890 | """ |
| 7891 | if doc.xref_is_stream(source): |
| 7892 | # read new xref stream, maintaining compression |
| 7893 | stream = doc.xref_stream_raw(source) |
| 7894 | doc.update_stream( |
| 7895 | target, |
| 7896 | stream, |
| 7897 | compress=False, # keeps source compression |
| 7898 | new=True, # in case target is no stream |
| 7899 | ) |
| 7900 | |
| 7901 | # empty the target completely, observe exceptions |
| 7902 | if keep is None: |
| 7903 | keep = [] |
| 7904 | for key in doc.xref_get_keys(target): |
| 7905 | if key in keep: |
| 7906 | continue |
| 7907 | doc.xref_set_key(target, key, "null") |
| 7908 | # copy over all source dict items |
| 7909 | for key in doc.xref_get_keys(source): |
| 7910 | item = doc.xref_get_key(source, key) |
| 7911 | doc.xref_set_key(target, key, item[1]) |
| 7912 | |
| 7913 | def xref_get_key(self, xref, key): |
| 7914 | """Get PDF dict key value of object at 'xref'.""" |
no test coverage detected