Add Watermarks to PDF in Python
Contents
[
Hide
]
Use WatermarkArtifact to add a text watermark to the first page of a PDF.
Add a Watermark to PDF
Set the text and style, then configure alignment, rotation, and opacity. Set is_background to place the watermark behind page content.
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)