Update watermarks

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.

Note
Updating of watermarks is not allowed in evaluation mode. Please set up a license as described in Licensing and evaluation.

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 Text property.
  5. Call the Save method to store the document in a new location.
using GroupDocs.Watermark;
using GroupDocs.Watermark.Search.SearchCriteria;
using GroupDocs.Watermark.Search;

using (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);
    Console.WriteLine("Found {0} possible watermark(s).", possibleWatermarks.Count);
    foreach (PossibleWatermark watermark in possibleWatermarks)
    {
        try
        {
            // Update text
            watermark.Text = "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 text 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 MaxDifference property. 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 ImageData property.
  6. Call the Save method to store the document in a new location.
using GroupDocs.Watermark;
using GroupDocs.Watermark.Search.SearchCriteria;
using GroupDocs.Watermark.Search;

using (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.MaxDifference = 0.9;
    PossibleWatermarkCollection possibleWatermarks = watermarker.Search(imageSearchCriteria);
    Console.WriteLine("Found {0} possible watermark(s).", possibleWatermarks.Count);
    
    // Read new watermark image
    byte[] imageData = File.ReadAllBytes("C:\\Docs\\new-logo.png");
    foreach (PossibleWatermark watermark in possibleWatermarks)
    {
        try
        {
            // Replace image
            watermark.ImageData = imageData;
        }
        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 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.