Adding watermark to images inside a document
Leave feedback
On this page
Beyond watermarking a document’s pages, you can watermark the images embedded inside it. Watermarker.get_images() returns a collection of every raster image in the document, regardless of format, and each image accepts its own watermark via add().
Watermark every embedded image
The example finds all embedded images, watermarks the larger ones (over 100×100), and saves the result.
fromgroupdocs.watermarkimportWatermarkerfromgroupdocs.watermark.watermarksimportTextWatermark,Font,Color,SizingTypefromgroupdocs.watermark.options.pdfimportPdfLoadOptionsdefwatermark_images_inside():withWatermarker("./document.pdf",PdfLoadOptions())aswatermarker:watermark=TextWatermark("PROTECTED",Font("Arial",8.0))watermark.foreground_color=Color.redwatermark.sizing_type=SizingType.SCALE_TO_PARENT_DIMENSIONSwatermark.scale_factor=1.0# Every raster image embedded in the documentforimageinwatermarker.get_images():# Skip tiny images such as icons or bulletsifimage.width>100andimage.height>100:image.add(watermark)watermarker.save("./output.pdf")if__name__=="__main__":watermark_images_inside()
document.pdf is the sample file used in this example. Click here to download it.
For the example document (10 embedded images), 8 images larger than 100×100 are watermarked. get_images() works the same way for Word, Excel, PowerPoint, and other formats — open the document and iterate the returned collection. You can also pass an ImageWatermark to image.add(...) to stamp a logo onto each embedded image.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.