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

DOC: Fix stamping example #2358

Merged
merged 1 commit into from
Dec 23, 2023
Merged
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
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