Skip to content

Commit

Permalink
DOC: Stamps and watermarks (#1082)
Browse files Browse the repository at this point in the history
Closes #307
Closes #410
  • Loading branch information
MartinThoma authored Jul 9, 2022
1 parent 9794ef6 commit b42e0db
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions docs/user/add-watermark.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
# Adding a Watermark to a PDF
# Adding a Stamp/Watermark to a PDF

Adding stamps or watermarks are two common ways to manipulate PDF files.
A stamp is adding something on top of the document, a watermark is in the
background of the document.

In both cases you might want to ensure that the mediabox/cropbox of the original
content stays the same.

## Stamp (Overlay)

```python
from PyPDF2 import PdfWriter, PdfReader


# Read the watermark
watermark = PdfReader("watermark.pdf")
def stamp(content_page, image_page):
"""Put the image over the content"""
# Note that this modifies the content_page in-place!
content_page.merge_page(image_page)
return content_page

# Read the page without watermark
reader = PdfReader("example.pdf")
page = reader.pages[0]

# Add the watermark to the page
page.merge_page(watermark.pages[0])
# Read the pages
reader_content = PdfReader("content.pdf")
reader_image = PdfReader("image.pdf")

# Add the page to the writer
# Modify it
modified = stamp(reader_content.pages[0], reader_image.pages[0])

# Create the new document
writer = PdfWriter()
writer.add_page(page)
writer.add_page(modified)
with open("out-stamp.pdf", "wb") as fp:
writer.write(fp)
```

![stamp.png](stamp.png)

## Watermark (Underlay)

```python
from PyPDF2 import PdfWriter, PdfReader


def watermark(content_page, image_page):
"""Put the image under the content"""
# Note that this modifies the image_page in-place!
image_page.merge_page(content_page)
return image_page


# finally, write the new document with a watermark
with open("PyPDF2-output.pdf", "wb") as fp:
# Read the pages
reader_content = PdfReader("content.pdf")
reader_image = PdfReader("image.pdf")

# Modify it
modified = stamp(reader_content.pages[0], reader_image.pages[0])

# Create the new document
writer = PdfWriter()
writer.add_page(modified)
with open("out-watermark.pdf", "wb") as fp:
writer.write(fp)
```

![watermark.png](watermark.png)
Binary file added docs/user/stamp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/user/watermark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b42e0db

Please sign in to comment.