Add Watermarks to PDF in Python
Contents
[
Hide
]
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
- Load the PDF document.
- Access page artifacts.
- Filter watermark artifacts.
- Collect watermark elements.
- Extract watermark properties.
- 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:
- Load the PDF document.
- Create a text state.
- Create a watermark artifact.
- Set watermark text and style.
- Configure positioning and rotation.
- Set opacity and background behavior.
- Attach the watermark to a page.
- 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.
- Load the PDF document.
- Access page artifacts.
- Filter watermark artifacts.
- Delete watermark artifacts.
- 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)