Watermarks in PDF document

A PDF watermark can be added in three different ways, each with its own trade-offs:

  • XObjects — added to the real page content. They are not removed by simple sanitization, which makes them the hardest to remove.
  • Artifacts — added as page artifacts. Use PdfArtifactWatermarkOptions.
  • Annotations — added as page annotations. Use PdfAnnotationWatermarkOptions; these can be made print-only so they appear when the document is printed but are hidden on screen.

Add artifact and annotation watermarks

The example below adds an artifact text watermark, an annotation text watermark, and a print-only annotation image watermark.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, ImageWatermark, Font
from groupdocs.watermark.common import HorizontalAlignment, VerticalAlignment
from groupdocs.watermark.options.pdf import (
    PdfLoadOptions, PdfArtifactWatermarkOptions, PdfAnnotationWatermarkOptions,
)

def add_pdf_watermarks():
    with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
        # Add as an artifact
        artifact_text = TextWatermark("Artifact", Font("Arial", 8.0))
        artifact_text.horizontal_alignment = HorizontalAlignment.RIGHT
        watermarker.add(artifact_text, PdfArtifactWatermarkOptions())

        # Add as an annotation
        annotation_text = TextWatermark("Annotation", Font("Arial", 8.0))
        annotation_text.horizontal_alignment = HorizontalAlignment.LEFT
        annotation_text.vertical_alignment = VerticalAlignment.TOP
        watermarker.add(annotation_text, PdfAnnotationWatermarkOptions())

        # Add a print-only annotation (visible when printed, hidden on screen)
        with ImageWatermark("./logo.png") as image_watermark:
            image_watermark.horizontal_alignment = HorizontalAlignment.RIGHT
            image_watermark.vertical_alignment = VerticalAlignment.TOP
            options = PdfAnnotationWatermarkOptions()
            options.print_only = True
            watermarker.add(image_watermark, options)

        watermarker.save("./output.pdf")

if __name__ == "__main__":
    add_pdf_watermarks()

document.pdf and logo.png are the sample files used in this example. Download document.pdf and logo.png.

Binary file (PDF, 528 KB)

Download full output

To work with watermarks already present in a PDF — extracting, modifying, or removing existing XObjects, artifacts, and annotations — see Existing objects in PDF document.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.