GroupDocs.Watermark also allows you to replace text and image in the found possible watermarks. Following sections will show you how to replace text and image of a found watermark.
Replacing text
To replace text of the found watermarks, loop through the possible watermarks in the watermark collection and call setText() method as shown in the following code sample.
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");TextSearchCriteriasearchCriteria=newTextSearchCriteria("test",false);PossibleWatermarkCollectionwatermarks=watermarker.search(searchCriteria);for(PossibleWatermarkwatermark:watermarks){try{// Edit text
watermark.setText("passed");}catch(Exceptione){// Found entity may not support text editing
// Passed argument can have inappropriate value
// Process such cases here
}}// Save document
watermarker.save("document.pdf");watermarker.close();
Replacing text with formatting
You can also replace the watermark’s text with formatting as shown in the below code sample.
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");TextSearchCriteriasearchCriteria=newTextSearchCriteria("test",false);PossibleWatermarkCollectionwatermarks=watermarker.search(searchCriteria);for(PossibleWatermarkwatermark:watermarks){try{// Edit text
watermark.getFormattedTextFragments().clear();watermark.getFormattedTextFragments().add("passed",newFont("Calibri",19,FontStyle.Bold),Color.getRed(),Color.getAqua());}catch(Exceptione){// Found entity may not support text editing
// Passed arguments can have inappropriate value
// Process such cases here
}}// Save document
watermarker.save("document.pdf");watermarker.close();
Replacing image
Following code sample shows how to replace the image of the found watermarks using GroupDocs.Watermark.
FileimageFile=newFile("image.png");byte[]imageData=newbyte[(int)imageFile.length()];InputStreamimageInputStream=newFileInputStream(imageFile);imageInputStream.read(imageData);imageInputStream.close();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf");// Search watermark matching a particular image
SearchCriteriasearchCriteria=newImageDctHashSearchCriteria("logo.bmp");PossibleWatermarkCollectionwatermarks=watermarker.search(searchCriteria);for(PossibleWatermarkwatermark:watermarks){try{// Replace image
watermark.setImageData(imageData);}catch(Exceptione){// Found entity may not support image replacement
// Passed image can have inappropriate format
// Process such cases here
}}// Save document
watermarker.save("document.pdf");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.