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.
// Create an instance of Watermarker
constwatermarker=newgroupdocsWatermark.Watermarker('sample.pdf');constwatermarks=watermarker.search();// Iterate through found watermarks
for(constwatermarkofwatermarks.getInnerList().toArray()){console.log(watermark.getText());console.log(watermark.getRotateAngle());console.log(watermark.getX());console.log(watermark.getY());console.log(watermark.getWidth());console.log(watermark.getHeight())}
Search criteria
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
// Create watermarker
constwatermarker=newgroupdocsWatermark.Watermarker('sample.pdf');// Define search criteria
constsearchCriteria=newgroupdocsWatermark.TextSearchCriteria("test",false);// Search for watermarks
constwatermarks=watermarker.search(searchCriteria);console.log(`Found ${watermarks.getCount()} possible watermark(s).`);
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.
// Create watermarker
constwatermarker=newgroupdocsWatermark.Watermarker('sample.pdf');// Initialize search criteria with the image
constimageSearchCriteria=newgroupdocsWatermark.ImageDctHashSearchCriteria('logo.png');// Set maximum allowed difference between images
imageSearchCriteria.setMaxDifference(0.9);// Search for possible watermarks matching the criteria
constpossibleWatermarks=watermarker.search(imageSearchCriteria);console.log(`Found ${possibleWatermarks.getCount()} possible watermark(s).`);
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:
Combined search criteria
GroupDocs.Watermark API also allows you to search watermarks by a combination of different search criteria. Following sample code shows how to search watermark with the combination of different search criteria.
// Create watermarker
constwatermarker=newgroupdocsWatermark.Watermarker('sample.pdf');// Define image search criteria
constimageSearchCriteria=newgroupdocsWatermark.ImageDctHashSearchCriteria('logo.png');imageSearchCriteria.setMaxDifference(0.9);// Define text search criteria
consttextSearchCriteria=newgroupdocsWatermark.TextSearchCriteria("Company Name");// Define rotate angle search criteria
constrotateAngleSearchCriteria=newgroupdocsWatermark.RotateAngleSearchCriteria(30,60);// Combine search criteria
constcombinedSearchCriteria=imageSearchCriteria.or(textSearchCriteria).and(rotateAngleSearchCriteria);// Search for possible watermarks
constpossibleWatermarks=watermarker.search(combinedSearchCriteria);// Output the results
console.log(`Found ${possibleWatermarks.getCount()} possible watermark(s).`);
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.