Adding watermark to images inside a document

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.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, Font, Color, SizingType
from groupdocs.watermark.options.pdf import PdfLoadOptions

def watermark_images_inside():
    with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
        watermark = TextWatermark("PROTECTED", Font("Arial", 8.0))
        watermark.foreground_color = Color.red
        watermark.sizing_type = SizingType.SCALE_TO_PARENT_DIMENSIONS
        watermark.scale_factor = 1.0

        # Every raster image embedded in the document
        for image in watermarker.get_images():
            # Skip tiny images such as icons or bullets
            if image.width > 100 and image.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.

Binary file (PDF, 1596 KB)

Download full output

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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.