Modifying found watermark properties

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Watermark MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-watermark -- dnx GroupDocs.Watermark.Mcp --yes
Folders, licensing and more →

GroupDocs.Watermark lets you replace the text or image of watermarks that already exist in a document. Search for the watermarks first, then assign a new value to each match.

Replace text

The example below opens a document that contains a “CONFIDENTIAL” watermark, finds it, and replaces the text with “APPROVED”.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.search.search_criteria import TextSearchCriteria

def modify_watermark():
    with Watermarker("./watermarked-document.pdf") as watermarker:
        possible = watermarker.search(TextSearchCriteria("CONFIDENTIAL"))
        for wm in possible:
            try:
                wm.text = "APPROVED"
            except Exception:
                # The entity may not support text editing, or the value is invalid
                pass
        watermarker.save("./output.pdf")

if __name__ == "__main__":
    modify_watermark()

watermarked-document.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 444 KB)

Download full output

Replace text with formatting

Use formatted_text_fragments to replace the text with styled fragments — choose the font, foreground color, and background color.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import Font, FontStyle, Color
from groupdocs.watermark.search.search_criteria import TextSearchCriteria

def modify_watermark_with_formatting():
    with Watermarker("./watermarked-document.pdf") as watermarker:
        possible = watermarker.search(TextSearchCriteria("CONFIDENTIAL"))
        for wm in possible:
            try:
                wm.formatted_text_fragments.clear()
                wm.formatted_text_fragments.add(
                    "APPROVED",
                    Font("Arial", 19.0, FontStyle.BOLD),
                    Color.red,
                    Color.aqua,
                )
            except Exception:
                # The entity may not support formatted text, or values may be invalid
                pass
        watermarker.save("./output.pdf")

if __name__ == "__main__":
    modify_watermark_with_formatting()

watermarked-document.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 445 KB)

Download full output

Replace image

Swap the image data of matched watermarks by assigning new bytes to image_data.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.search.search_criteria import ImageDctHashSearchCriteria

def replace_watermark_image():
    with open("./stamp.png", "rb") as f:
        image_data = f.read()

    with Watermarker("./watermarked-document.docx") as watermarker:
        criteria = ImageDctHashSearchCriteria("./logo.png")
        criteria.max_difference = 0.9
        possible = watermarker.search(criteria)
        for wm in possible:
            try:
                wm.image_data = image_data
            except Exception:
                # The entity may not support image replacement, or the image format is invalid
                pass
        watermarker.save("./output.docx")

if __name__ == "__main__":
    replace_watermark_image()

watermarked-document.docx, logo.png, and stamp.png are the sample files used in this example. Download watermarked-document.docx, logo.png, and stamp.png.

Binary file (DOCX, 19 KB)

Download full output