Skip to content

Commit

Permalink
ENH: Extend PdfWriter.append() to PageObjects (#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz authored Mar 18, 2023
1 parent 3419429 commit af8d7c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 15 additions & 5 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ def set_need_appearances_writer(self) -> None:
try:
# get the AcroForm tree
if CatalogDictionary.ACRO_FORM not in self._root_object:
self._root_object[NameObject(CatalogDictionary.ACRO_FORM)] = self._add_object(DictionaryObject())
self._root_object[
NameObject(CatalogDictionary.ACRO_FORM)
] = self._add_object(DictionaryObject())

need_appearances = NameObject(InteractiveFormDictEntries.NeedAppearances)
self._root_object[CatalogDictionary.ACRO_FORM][need_appearances] = BooleanObject(True) # type: ignore
Expand Down Expand Up @@ -2509,7 +2511,12 @@ def append(
str, None, PageRange, Tuple[int, int], Tuple[int, int, int], List[int]
] = None,
pages: Union[
None, PageRange, Tuple[int, int], Tuple[int, int, int], List[int]
None,
PageRange,
Tuple[int, int],
Tuple[int, int, int],
List[int],
List[PageObject],
] = None,
import_outline: bool = True,
excluded_fields: Optional[Union[List[str], Tuple[str, ...]]] = None,
Expand Down Expand Up @@ -2570,7 +2577,7 @@ def merge(
position: Optional[int],
fileobj: Union[Path, StrByteType, PdfReader],
outline_item: Optional[str] = None,
pages: Optional[PageRangeSpec] = None,
pages: Optional[Union[PageRangeSpec, List[PageObject]]] = None,
import_outline: bool = True,
excluded_fields: Optional[Union[List[str], Tuple[str, ...]]] = (),
) -> None:
Expand Down Expand Up @@ -2627,8 +2634,11 @@ def merge(
)

srcpages = {}
for i in pages:
pg = reader.pages[i]
for page in pages:
if isinstance(page, PageObject):
pg = page
else:
pg = reader.pages[page]
assert pg.indirect_reference is not None
if position is None:
srcpages[pg.indirect_reference.idnum] = self.add_page(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,9 @@ def test_reset_translation():
writer.reset_translation()
writer.append(reader, (0, 10))
assert len(writer._objects) >= nb + 200
nb = len(writer._objects)
nb = len(writer.pages)
writer.append(reader, [reader.pages[0], reader.pages[0]])
assert len(writer.pages) == nb + 2


def test_threads_empty():
Expand Down

0 comments on commit af8d7c4

Please sign in to comment.