Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active November 30, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/fab72765f204598394779233a3a96e66 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/fab72765f204598394779233a3a96e66 to your computer and use it in GitHub Desktop.
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