Add Watermarks to PDF in Python

Add a watermark artifact to a PDF Document using Aspose.PDF for Python via .NET. A watermark is a visual overlay applied to pages for branding, security, or informational purposes. The example shows how to configure TextState appearance, positioning with HorizontalAlignment and VerticalAlignment, rotation, and transparency before applying the watermark to a Page.

Extract Watermarks from PDF

  1. Load the PDF document.
  2. Access page artifacts.
  3. Filter watermark artifacts.
  4. Collect watermark elements.
  5. Extract watermark properties.
  6. Output watermark information.
from os import path
import sys
import aspose.pdf as ap

def extract_watermark_from_pdf(infile):
    with ap.Document(infile) as document:
        watermarks = [
            artifact
            for artifact in document.pages[1].artifacts
            if artifact.type == ap.Artifact.ArtifactType.PAGINATION
            and artifact.subtype == ap.Artifact.ArtifactSubtype.WATERMARK
        ]

        for watermark in watermarks:
            print(f"{watermark.text} {watermark.rectangle}")

Add a Watermark to PDF

Add a text watermark to a PDF document using Aspose.PDF for Python:

  1. Load the PDF document.
  2. Create a text state.
  3. Create a watermark artifact.
  4. Set watermark text and style.
  5. Configure positioning and rotation.
  6. Set opacity and background behavior.
  7. Attach the watermark to a page.
  8. Save the updated document.
from os import path
import sys
import aspose.pdf as ap

def add_watermark_artifact(infile, outfile):
    with ap.Document(infile) as document:
        text_state = ap.text.TextState()
        text_state.font_size = 72
        text_state.foreground_color = ap.Color.blue_violet
        text_state.font_style = ap.text.FontStyles.BOLD
        text_state.font = ap.text.FontRepository.find_font("Arial")

        watermark = ap.WatermarkArtifact()
        watermark.set_text_and_state("WATERMARK", text_state)
        watermark.artifact_horizontal_alignment = ap.HorizontalAlignment.CENTER
        watermark.artifact_vertical_alignment = ap.VerticalAlignment.CENTER
        watermark.rotation = 60
        watermark.opacity = 0.2
        watermark.is_background = True

        document.pages[1].artifacts.append(watermark)
        document.save(outfile)

Remove Watermark Artifacts from PDF Page

Remove watermark artifacts from a specific page in a PDF document using the Aspose.PDF for Python API. The approach targets watermark elements stored as page artifacts (specifically those classified as pagination watermark subtypes), iterates through them, and deletes them before saving the updated document.

  1. Load the PDF document.
  2. Access page artifacts.
  3. Filter watermark artifacts.
  4. Delete watermark artifacts.
  5. Save the updated document.
from os import path
import sys
import aspose.pdf as ap

def delete_watermark_artifact(infile, outfile):
    with ap.Document(infile) as document:
        watermarks = [
            artifact
            for artifact in document.pages[1].artifacts
            if artifact.type == ap.Artifact.ArtifactType.PAGINATION
            and artifact.subtype == ap.Artifact.ArtifactSubtype.WATERMARK
        ]

        for watermark in watermarks:
            document.pages[1].artifacts.delete(watermark)

        document.save(outfile)