Update watermarks

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 →

The GroupDocs.Watermark library is also capable of searching the document for any existing text or image watermarks. These can be watermarks added by any third-party tool, not only by GroupDocs.Watermark. Searching watermarks is possible for some of the supported formats. To learn whether it is available for your format, check Supported formats.

Once found you can update those watermarks.

Updating text watermarks

To search and update text watermarks:

  1. Create an instance of the Watermarker class for a local file or file stream;
  2. Specify the sought-after text of the watermarks. To define criteria use the instance of the TextSearchCriteria class. In this example, we will search for a particular string match.
  3. Call the search method of the Watermarker class to perform the search and obtain a collection of all possible watermarks that meet the search criteria.
  4. Iterate through the collection of possible watermarks and update the found text occurrences using the setText method.
  5. Call the save method to store the document in a new location.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.search.PossibleWatermark;
import com.groupdocs.watermark.search.PossibleWatermarkCollection;
import com.groupdocs.watermark.search.TextSearchCriteria;

try (Watermarker watermarker = new Watermarker("C:\\Docs\\watermarked-sample.docx")) {
    // Search watermark matching a particular text
    TextSearchCriteria searchCriteria = new TextSearchCriteria("Contract Draft", false);
    PossibleWatermarkCollection possibleWatermarks = watermarker.search(searchCriteria);
    System.out.println("Found " + possibleWatermarks.getCount() + " possible watermark(s).");
    for (PossibleWatermark watermark : possibleWatermarks) {
        try {
            // Update text
            watermark.setText("Contract is no longer valid");
        } catch (Exception e) {
            // Found entity may not support text editing
            // Passed argument can have inappropriate value
            // Process such cases here
        }
    }
    // Save document
    watermarker.save("C:\\Docs\\updated-sample.docx");
}

Run the program. All found occurrences of “Contract Draft” in watermarks will be changed to “Contract is no longer valid”.

Updating text watermarks

What’s next

Text search capabilities of GroupDocs.Watermark are not limited by simple text matches. To learn how to search using regular expressions, or search the watermarks on the basis of some particular text formatting, or update the formatting of found text, see the Searching watermarks and Modifying found watermark properties articles of the “Advanced usage” section.

Updating image watermarks

To search and update image watermarks:

  1. Create an instance of the Watermarker class for a local file or file stream;
  2. Specify the sought-after image of the watermarks. To define criteria use the instance of the ImageSearchCriteria class.
  3. Specify the maximum allowed difference between the images using the setMaxDifference method. Too strict threshold may fail to detect all needed images, while a very lenient threshold could match dissimilar images.
  4. Call the search method of the Watermarker class to perform the search and obtain a collection of all possible watermarks that meet the search criteria.
  5. Iterate through the collection of possible watermarks and update the found image occurrences using the setImageData method.
  6. Call the save method to store the document in a new location.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.search.ImageDctHashSearchCriteria;
import com.groupdocs.watermark.search.ImageSearchCriteria;
import com.groupdocs.watermark.search.PossibleWatermark;
import com.groupdocs.watermark.search.PossibleWatermarkCollection;

import java.nio.file.Files;
import java.nio.file.Paths;

// Files.readAllBytes throws IOException: declare it in the enclosing method or catch it.
try (Watermarker watermarker = new Watermarker("C:\\Docs\\watermarked-sample.docx")) {
    // Initialize criteria with the image
    ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("C:\\Docs\\logo.png");
    // Set maximum allowed difference between images
    imageSearchCriteria.setMaxDifference(0.9);
    PossibleWatermarkCollection possibleWatermarks = watermarker.search(imageSearchCriteria);
    System.out.println("Found " + possibleWatermarks.getCount() + " possible watermark(s).");

    // Read new watermark image
    byte[] imageData = Files.readAllBytes(Paths.get("C:\\Docs\\new-logo.png"));
    for (PossibleWatermark watermark : possibleWatermarks) {
        try {
            // Replace image
            watermark.setImageData(imageData);
        } catch (Exception e) {
            // Found entity may not support image replacement
            // Passed image can have inappropriate format
            // Process such cases here
        }
    }
    // Save document
    watermarker.save("C:\\Docs\\updated-sample.docx");
}

Run the program. All found occurrences of image watermarks will be updated.

Updating image watermarks

What’s next

Searching by images is a way more complex operation that may require finding an appropriate difference threshold or even using other image comparison algorithms. GroupDocs.Watermark offers several image search criteria algorithms. To learn more about them, see the Searching watermarks article of the “Advanced usage” section.