Work with Watermark in PDF via Python

Work with watermarks in PDF document programmatically using Aspose.PDF for Python for .NET Library

Most popular action with Watermarks in Python

Add Watermark with Python for .NET Library

In order to add Watermark to PDF File, we’ll use Aspose.PDF for Python API, which is a feature-rich, powerful, and easy-to-use document manipulation API for .NET. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Add Watermark using Python


To try the code in your environment, you need Aspose.PDF for Python.

  1. Load the PDF with an instance of Document.
  2. Create an instance of WatermarkArtifact.
  3. Set properties of WatermarkArtifact object.
  4. Add watermark using method Add of Aspose.Pdf.Page.Artifacts collection class.
  5. Save PDF file

Add Watermark in PDF - Python

This sample code shows how to add Watermark to PDF Pages - Python

Input file:

File not added

Output format:

Output file:

    import aspose.pdf as ap 

    input_pdf = DIR_INPUT_PAGES + "add_watermark.pdf"
    output_pdf = DIR_OUTPUT_PAGES + "add_watermark.pdf"

    document = ap.Document(input_pdf)
    artifact = ap.WatermarkArtifact()

    ts = ap.text.TextState()
    ts.font_size = 72
    ts.foreground_color = ap.Color.blue
    ts.font = ap.text.FontRepository.find_font("Courier")

    artifact.set_text_and_state("WATERMARK", ts)
    artifact.artifact_horizontal_alignment = ap.HorizontalAlignment.CENTER
    artifact.artifact_vertical_alignment = ap.VerticalAlignment.CENTER
    artifact.rotation = 45
    artifact.opacity = 0.5
    artifact.is_background = True
    document.pages[1].artifacts.append(artifact)
    document.save(output_pdf)