GroupDocs.Watermark API allows you to search the possible watermarks placed in any document. You can also search the watermarks that are added using some third-party tool. The API provides Search method to search watermarks in a whole document or in any part of the document. Following code snippet shows how to find and get all possible watermarks in a document.
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf")){PossibleWatermarkCollectionpossibleWatermarks=watermarker.Search();foreach(PossibleWatermarkpossibleWatermarkinpossibleWatermarks){if(possibleWatermark.ImageData!=null){Console.WriteLine(possibleWatermark.ImageData.Length);}Console.WriteLine($"Text {possibleWatermark.Text}");Console.WriteLine($"X {possibleWatermark.X}");Console.WriteLine($"Y {possibleWatermark.Y}");Console.WriteLine($"RotateAngle {possibleWatermark.RotateAngle}");Console.WriteLine($"Width {possibleWatermark.Width}");Console.WriteLine($"Height {possibleWatermark.Height}");Console.WriteLine($"PageNumber {possibleWatermark.PageNumber}");Console.WriteLine("");}}
Search criteria
Usually, large documents may contain too many objects which can be considered as watermarks. Parameterless overload of Search method returns only some of them, e.g. backgrounds or floating objects which could have been added during document post-processing. You can use search criteria to find objects with some specific parameters.
Text search criteria
Following code snippet shows how to search for the watermarks that meet a particular text criterion.
What happens when the user is passing TextSearchCriteria instance to the method?
It searches fragments of document’s main text which match regular expression (or contain exact search string)
It checks text of other objects (shapes, XObjects, annotations etc.) if they match regular expression (or contain exact search string)
Search in the main text of a document is performed only if you pass TextSearchCriteria instance to Search method.
Image search criteria
Sometimes a document can contain image watermarks, and it’s necessary to find them using sample picture. For example, you may want to find all possible image watermarks that are similar to a company logo. Following sample code searches for image watermarks that resemble with a particular image using.
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf")){// Initialize criteria with the imageImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("watermark.jpg");//Set maximum allowed difference between imagesimageSearchCriteria.MaxDifference=0.9;PossibleWatermarkCollectionpossibleWatermarks=watermarker.Search(imageSearchCriteria);Console.WriteLine("Found {0} possible watermark(s).",possibleWatermarks.Count);}
MaxDifference property is used to set maximum allowed difference between sample image and possible watermark. The value should be between 0 and 1. The value 0 means that only identical images will be found.
Using of ImageDctHashSearchCriteria is the most efficient way to find image watermark by a sample. This criterion uses DCT (Discrete Cosine Transform) based perceptual hash for image similarity comparison. But there are other image search criteria that are based on other algorithms:
ImageColorHistogramSearchCriteria uses image color histograms for calculating image similarity. This criterion is invariant to rotation, scaling, and translation of the image.
ImageThumbnailSearchCriteria uses image binarized thumbnail for calculating image similarity. This criterion is invariant to rotation, scaling and insignificant changes of the color palette.
Combined search criteria
GroupDocs.Watermark API also allows you to search watermarks by a combination (And, Or, Not) of different search criteria. Following sample code shows how to search watermark with the combination of different search criteria.
using(Watermarkerwatermarker=newWatermarker("document.pdf")){ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");imageSearchCriteria.MaxDifference=0.9;TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");RotateAngleSearchCriteriarotateAngleSearchCriteria=newRotateAngleSearchCriteria(30,60);SearchCriteriacombinedSearchCriteria=imageSearchCriteria.Or(textSearchCriteria).And(rotateAngleSearchCriteria);PossibleWatermarkCollectionpossibleWatermarks=watermarker.Search(combinedSearchCriteria);Console.WriteLine("Found {0} possible watermark(s).",possibleWatermarks.Count);}
Text formatting search criteria
GroupDocs.Watermark also enables you to search the watermarks on the basis of some particular text formatting. You can provide a search criterion containing font name, size, color etc and the API will find the watermarks with matching properties. Following code snippet shows how to search watermark with a particular text formatting.
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf")){TextFormattingSearchCriteriacriteria=newTextFormattingSearchCriteria();criteria.ForegroundColorRange=newColorRange();criteria.ForegroundColorRange.MinHue=-5;criteria.ForegroundColorRange.MaxHue=10;criteria.ForegroundColorRange.MinBrightness=0.01f;criteria.ForegroundColorRange.MaxBrightness=0.99f;criteria.BackgroundColorRange=newColorRange();criteria.BackgroundColorRange.IsEmpty=true;criteria.FontName="Arial";criteria.MinFontSize=19;criteria.MaxFontSize=42;criteria.FontBold=true;PossibleWatermarkCollectionwatermarks=watermarker.Search(criteria);// The code for working with found watermarks goes here.Console.WriteLine("Found {0} possible watermark(s).",watermarks.Count);}
Searching watermarks in particular objects
This feature allows you to specify which objects should be included in watermark search. Restricting searchable objects, you can significantly increase search performance. Following sample code shows how to set searchable objects globally (for all documents that will be created after that).
WatermarkerSettingssettings=newWatermarkerSettings();settings.SearchableObjects=newSearchableObjects{WordProcessingSearchableObjects=WordProcessingSearchableObjects.Hyperlinks|WordProcessingSearchableObjects.Text,SpreadsheetSearchableObjects=SpreadsheetSearchableObjects.HeadersFooters,PresentationSearchableObjects=PresentationSearchableObjects.SlidesBackgrounds|PresentationSearchableObjects.Shapes,DiagramSearchableObjects=DiagramSearchableObjects.None,PdfSearchableObjects=PdfSearchableObjects.All};string[]files={"document.docx","spreadsheet.xlsx","presentation.pptx","diagram.vsdx","document.pdf"};foreach(stringfileinfiles){using(Watermarkerwatermarker=newWatermarker(file,settings)){PossibleWatermarkCollectionwatermarks=watermarker.Search();// The code for working with found watermarks goes here.Console.WriteLine("In {0} found {1} possible watermark(s).",Path.GetFileName(file),watermarks.Count);}}
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf")){// Search for hyperlinks only.watermarker.SearchableObjects.PdfSearchableObjects=PdfSearchableObjects.Hyperlinks;PossibleWatermarkCollectionwatermarks=watermarker.Search();// The code for working with found watermarks goes here.Console.WriteLine("Found {0} hyperlink watermark(s).",watermarks.Count);}
Searching text watermark skipping unreadable characters
This feature allows finding text watermark even if it contains unreadable characters between the letters. The following code sample shows how to skip unreadable characters when searching for the watermark.
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf")){stringwatermarkText="Company name";TextSearchCriteriacriterion=newTextSearchCriteria(watermarkText);// Enable skipping of unreadable characterscriterion.SkipUnreadableCharacters=true;PossibleWatermarkCollectionresult=watermarker.Search(criterion);Console.WriteLine("Found {0} possible watermark(s).",result.Count);}
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.