Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Extend PdfWriter.append() to PageObjects #1704

Merged
merged 1 commit into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2486,7 +2488,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 @@ -2547,7 +2554,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 @@ -2604,8 +2611,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 @@ -987,7 +987,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