GroupDocs.Watermark gives you fine-grained control over watermarks in PDF documents. Open the PDF with PdfLoadOptions and pass a PDF-specific watermark option (such as PdfArtifactWatermarkOptions or PdfAnnotationWatermarkOptions) to add() to target a particular page.
Add a watermark to a particular page
Use PdfArtifactWatermarkOptions and set page_index (0-based) to place a watermark on a specific page. The example below adds a text watermark to the first page and an image watermark to the second.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportTextWatermark,ImageWatermark,Font,Colorfromgroupdocs.watermark.commonimportHorizontalAlignment,VerticalAlignmentfromgroupdocs.watermark.options.pdfimportPdfLoadOptions,PdfArtifactWatermarkOptionsdefadd_watermark_to_page():withWatermarker("./document.pdf",PdfLoadOptions())aswatermarker:# Text watermark on the first page (page_index is 0-based)text_watermark=TextWatermark("CONFIDENTIAL",Font("Arial",19.0))text_watermark.foreground_color=Color.redtext_watermark.horizontal_alignment=HorizontalAlignment.CENTERtext_watermark.vertical_alignment=VerticalAlignment.CENTERtext_options=PdfArtifactWatermarkOptions()text_options.page_index=0watermarker.add(text_watermark,text_options)# Image watermark on the second pagewithImageWatermark("./logo.png")asimage_watermark:image_watermark.horizontal_alignment=HorizontalAlignment.RIGHTimage_watermark.vertical_alignment=VerticalAlignment.TOPimage_options=PdfArtifactWatermarkOptions()image_options.page_index=1watermarker.add(image_watermark,image_options)watermarker.save("./output.pdf")if__name__=="__main__":add_watermark_to_page()
document.pdf (two pages) and logo.png are the sample files used in this example. Download document.pdf and logo.png.
PdfAnnotationWatermarkOptions works the same way and adds the watermark as a page annotation instead of an artifact; see Watermarks in PDF document.
Working with existing PDF content
Operations that read or modify the PDF content tree — watermarking the existing images on a page, reading a page’s size, applying PDF page margins, and working with PDF attachments — are accessed through Watermarker.get_content(), which returns a PdfContent exposing pages, attachments, and more.
Add a watermark to all images of a particular page