Add watermark to PDF using Python

Aspose.PDF for Python via .NET allows adding watermarks to your PDF document using Artifacts. Please check this article to resolve your task.

In order to work with artifacts, Aspose.PDF has two classes: Artifact and ArtifactCollection.

In order to get all artifacts on a particular page, the Page class has the Artifacts property. This topic explains how to work with artifact in PDF files.

Working with Artifacts

The Artifact class contains following properties:

contents – Gets a collection of artifact internal operators. Its supported type is System.Collections.ICollection. form – Gets an artifact’s XForm (if XForm is used). Watermarks, header, and footer artifacts contains XForm which shows all artifact contents. image – Gets an artifact’s image (if an image is presents, else null). text – Gets an artifact’s text. rectangle – Gets an position of an artifact on the page. rotation – Gets an artifact’s rotation (in degrees, positive value indicates counter-clockwise rotation). opacity – Gets an artifact’s opacity. Possible values are in the range 0…1, where 1 is completely opaque.

Programming Samples: How To Add Watermark On PDF Files

The following code snippet shows how to get each watermark on the first page of a PDF file with Python.


    import aspose.pdf as ap

    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)