Skip to content

Commit

Permalink
DOC: Fix stamping example (#2358)
Browse files Browse the repository at this point in the history
See https://patch-diff.githubusercontent.com/raw/py-pdf/pypdf/pull/1945

Co-authored-by: dmjohnsson23 <dmjohn235@gmail.com>
  • Loading branch information
MartinThoma and dmjohnsson23 authored Dec 23, 2023
1 parent c43ca15 commit 0a79b3c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions docs/user/add-watermark.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ Else use `merge_transformed_page()` with `Transformation()` if you need to trans

```python
from pathlib import Path
from typing import Union, Literal, List
from typing import List, Union

from pypdf import PdfWriter, PdfReader
from pypdf import PdfReader, PdfWriter, Transformation


def stamp(
content_pdf: Path,
stamp_pdf: Path,
pdf_result: Path,
page_indices: Union[Literal["ALL"], List[int]] = "ALL",
content_pdf: Union[Path, str],
stamp_pdf: Union[Path, str],
pdf_result: Union[Path, str],
page_indices: Union[None, List[int]] = None,
):
stamp_page = PdfReader(stamp_pdf).pages[0]

writer = PdfWriter()
# page_indices can be a List(array) of page, tuples are for range definition
writer.append(content, pages=None if page_indices == "ALL" else page_indices)
reader = PdfReader(content_pdf)
writer.append(reader, pages=page_indices)

for content_page in writer.pages:
content_page.merge_transformed_page(
Expand All @@ -49,9 +50,14 @@ def stamp(
)

writer.write(pdf_result)


stamp("example.pdf", "stamp.pdf", "out.pdf")
```

If you are experiencing wrongly rotated watermarks/stamps, try to use `transfer_rotation_to_content()` on the corresponding pages beforehand to fix the page boxes.
If you are experiencing wrongly rotated watermarks/stamps, try to use
`transfer_rotation_to_content()` on the corresponding pages beforehand
to fix the page boxes.

Example of stamp:
![stamp.png](stamp.png)
Expand Down

0 comments on commit 0a79b3c

Please sign in to comment.