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"
Watermarkerwatermarker=newWatermarker("document.pdf");PossibleWatermarkCollectionpossibleWatermarks=watermarker.search();for(PossibleWatermarkpossibleWatermark:possibleWatermarks){if(possibleWatermark.getImageData()!=null){System.out.println(possibleWatermark.getImageData().length);}System.out.println(possibleWatermark.getText());System.out.println(possibleWatermark.getX());System.out.println(possibleWatermark.getY());System.out.println(possibleWatermark.getRotateAngle());System.out.println(possibleWatermark.getWidth());System.out.println(possibleWatermark.getHeight());}watermarker.close();
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.
advanced_usage.searching_and_modifying_watermarks.SearchImageWatermark
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");// Initialize criteria with the image
ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("watermark.jpg");//Set maximum allowed difference between images
imageSearchCriteria.setMaxDifference(0.9);PossibleWatermarkCollectionpossibleWatermarks=watermarker.search(imageSearchCriteria);System.out.println("Found "+possibleWatermarks.getCount()+" possible watermark(s).");watermarker.close();
setMaxDifference() method 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.
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");imageSearchCriteria.setMaxDifference(0.9);TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");RotateAngleSearchCriteriarotateAngleSearchCriteria=newRotateAngleSearchCriteria(30,60);SearchCriteriacombinedSearchCriteria=imageSearchCriteria.or(textSearchCriteria).and(rotateAngleSearchCriteria);PossibleWatermarkCollectionpossibleWatermarks=watermarker.search(combinedSearchCriteria);System.out.println("Found "+possibleWatermarks.getCount()+" possible watermark(s).");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.pdf");TextFormattingSearchCriteriacriteria=newTextFormattingSearchCriteria();criteria.setForegroundColorRange(newColorRange());criteria.getForegroundColorRange().setMinHue(-5);criteria.getForegroundColorRange().setMaxHue(10);criteria.getForegroundColorRange().setMinBrightness(0.01f);criteria.getForegroundColorRange().setMaxBrightness(0.99f);criteria.setBackgroundColorRange(newColorRange());criteria.getBackgroundColorRange().setEmpty(true);criteria.setFontName("Arial");criteria.setMinFontSize(19);criteria.setMaxFontSize(42);criteria.setFontBold(true);PossibleWatermarkCollectionwatermarks=watermarker.search(criteria);// The code for working with found watermarks goes here.
System.out.println("Found "+watermarks.getCount()+" possible watermark(s).");watermarker.close();
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.setSearchableObjects(newSearchableObjects());settings.getSearchableObjects().setWordProcessingSearchableObjects(WordProcessingSearchableObjects.Hyperlinks|WordProcessingSearchableObjects.Text);settings.getSearchableObjects().setSpreadsheetSearchableObjects(SpreadsheetSearchableObjects.HeadersFooters);settings.getSearchableObjects().setPresentationSearchableObjects(PresentationSearchableObjects.SlidesBackgrounds|PresentationSearchableObjects.Shapes);settings.getSearchableObjects().setDiagramSearchableObjects(DiagramSearchableObjects.None);settings.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.All);String[]files={"document.docx","spreadsheet.xlsx","presentation.pptx","diagram.vsdx","document.pdf"};for(Stringfile:files){Watermarkerwatermarker=newWatermarker(file,settings);PossibleWatermarkCollectionwatermarks=watermarker.search();// The code for working with found watermarks goes here.
System.out.println("In "+newFile(file).getName()+" found "+watermarks.getCount()+" possible watermark(s).");watermarker.close();}
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");// Search for hyperlinks only.
watermarker.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.Hyperlinks);PossibleWatermarkCollectionwatermarks=watermarker.search();// The code for working with found watermarks goes here.
System.out.println("Found "+watermarks.getCount()+" possible watermark(s).");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.pdf");StringwatermarkText="Company name";TextSearchCriteriacriterion=newTextSearchCriteria(watermarkText);// Enable skipping of unreadable characters
criterion.setSkipUnreadableCharacters(true);PossibleWatermarkCollectionresult=watermarker.search(criterion);// ...
System.out.println("Found "+result.getCount()+" possible watermark(s).");watermarker.close();
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.