Last active
November 30, 2020 11:44
-
-
Save GroupDocsGists/fab72765f204598394779233a3a96e66 to your computer and use it in GitHub Desktop.
How to remove watermarks from documents in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Watermarker watermarker = new Watermarker("filepath/watermarked.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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