Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to remove watermarks from documents in Java
// Configure search criterion for image watermark
ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("filepath/watermark.png");
imageSearchCriteria.setMaxDifference(0.2); // Set how much the watermark can differ from the provided image.
// Configure search criterion for text watermark
TextSearchCriteria textSearchCriteria = new TextSearchCriteria("CONFIDENTIAL");
// Combining the text and image search criteria
SearchCriteria combinedSearchCriteria = imageSearchCriteria.or(textSearchCriteria);
PossibleWatermarkCollection possibleWatermarks = watermarker.search(combinedSearchCriteria);
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.search.ImageDctHashSearchCriteria;
import com.groupdocs.watermark.search.ImageSearchCriteria;
import com.groupdocs.watermark.search.PossibleWatermarkCollection;
import com.groupdocs.watermark.search.SearchCriteria;
import com.groupdocs.watermark.search.TextSearchCriteria;
Watermarker watermarker = new Watermarker("filepath/watermarked.pdf");
//Iterate through the possible watermarks collection, check, and remove watermarks
while(possibleWatermarks.getCount()>0)
{
if (possibleWatermarks.get_Item(0).getImageData() != null)
{
possibleWatermarks.removeAt(0);
System.out.println("Removed Image Watermark.");
}
else
{
possibleWatermarks.removeAt(0);
System.out.println("Removed Text Watermark.");
}
}
// Find and Remove Watermarks from PDF, Word, Excel, PowerPoint, and Visio documents in Java
Watermarker watermarker = new Watermarker("filepath/watermarked.pdf"); // Provide any supported document
// Configure search criterion for image watermark
ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("filepath/watermark.png");
imageSearchCriteria.setMaxDifference(0.2); // Set how much the watermark can differ from the provided image.
// Configure search criterion for text watermark
TextSearchCriteria textSearchCriteria = new TextSearchCriteria("CONFIDENTIAL");
// Combining the text and image search criteria
SearchCriteria combinedSearchCriteria = imageSearchCriteria.or(textSearchCriteria);
PossibleWatermarkCollection possibleWatermarks = watermarker.search(combinedSearchCriteria);
//Iterate through the possible watermarks collection, check, and remove watermarks
while(possibleWatermarks.getCount()>0)
{
if (possibleWatermarks.get_Item(0).getImageData() != null)
{
possibleWatermarks.removeAt(0);
System.out.println("Removed Image Watermark.");
}
else
{
possibleWatermarks.removeAt(0);
System.out.println("Removed Text Watermark.");
}
}
watermarker.save("filepath/without_watermark.pdf");
watermarker.close();
watermarker.save("filepath/without_watermark.pdf");
watermarker.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.