Migration Notes
Leave feedback
Here are the key reasons to use the new updated API provided by GroupDocs.Watermark for Java since version 20.1:
- Watermarker class is introduced as a single entry point to manage watermarks in the document (instead of Documentclass from previous versions).
- Adding watermarks was unified for all supported document formats.
- Product architecture was redesigned from scratch in order to simplify passing options to manage watermarks.
- Document information and preview generation procedures were simplified.
Here is brief comparison of how to manage watermarks using the old and new API.
The folowing examples show how to load a document of any supported format.
Old API
Document doc = Document.load("test.doc");
// watermarking goes here
// ...
doc.close();
New API
Watermarker watermarker = new Watermarker("test.doc");
// watermarking goes here
// ...
doc.close();
The following examples show how to load a diagram document.
Old API
DiagramDocument doc = Document.load(DiagramDocument.class, "diagram.vsdx");
// watermarking goes here
// ...
doc.close();
New API
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
Watermarker watermarker = new Watermarker("diagram.vsdx", loadOptions);
// watermarking goes here
// ...
doc.close();
The following examples show how to add text watermark to a document of any supported type.
Old API
for (File file : new File("Documents").listFiles())
{
Document document = Document.load(file.getPath());
TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36))
watermark.setForegroundColor(Color.getRed());
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
document.addWatermark(watermark);
document.save(file.getPath() + ".watermarked");
document.close();
}
New API
for (File file : new File("Documents").listFiles())
{
Watermarker watermarker = new Watermarker(file.getPath());
TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));
watermark.setForegroundColor(Color.getRed());
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
watermarker.add(watermark);
watermarker.save();
watermarker.close();
}
The following examples show how to add watermark to the first page of a diagram document.
Old API
DiagramDocument doc = Document.load(DiagramDocument.class, "diagram.vsdx");
TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Calibri", 19));
doc.getPages().get_Item(0).addWatermark(textWatermark);
doc.save();
doc.close();
New API
Watermarker watermarker = new Watermarker("diagram.vsdx", new DiagramLoadOptions());
TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Calibri", 19));
DiagramPageWatermarkOptions options = new DiagramPageWatermarkOptions();
options.setPageIndex(0);
watermarker.add(textWatermark, options);
watermarker.save();
watermarker.close();
The following examles show how to find watermarks using search criteria.
Old API
Document doc = Document.load("test.some_ext");
SizeSearchCriteria widthRange = new SizeSearchCriteria(Dimension.Width, 50, 100);
RotateAngleSearchCriteria rotateAngle = new RotateAngleSearchCriteria(0, 45);
TextSearchCriteria textCriteria = new TextSearchCriteria(Pattern.compile("^Test watermark$"));
PossibleWatermarkCollection watermarks = doc.findWatermarks(textCriteria.and(widthRange.or(rotateAngle)));
System.out.println("Found " + watermarks.Count + " possible watermarks.");
doc.close();
New API
Watermarker watermarker = new Watermarker("test.some_ext");
SizeSearchCriteria widthRange = new SizeSearchCriteria(Dimension.Width, 50, 100);
RotateAngleSearchCriteria rotateAngle = new RotateAngleSearchCriteria(0, 45);
TextSearchCriteria textCriteria = new TextSearchCriteria(Pattern.compile("^Test watermark$"));
PossibleWatermarkCollection watermarks = watermarker.search(textCriteria.and(widthRange.or(rotateAngle)));
System.out.println("Found " + watermarks.Count + " possible watermarks.");
watermarker.close();
The following examples show how to remove all possible watermarks.
Old API
Document doc = Document.load("document.pdf");
PossibleWatermarkCollection watermarks = doc.findWatermarks();
watermarks.clear();
doc.save("document_without_watermarks.pdf");
doc.close();
New API
Watermarker watermarker = new Watermarker("document.pdf");
PossibleWatermarkCollection watermarks = watermarker.search();
watermarker.remove(watermarks);
watermarker.save("document_without_watermarks.pdf");
watermarker.close();
The following examples show how to get document information from the local file.
Old API
DocumentInfo documentInfo = Document.getInfo("test.ppt");
System.out.println(documentInfo.getFileFormat());
System.out.println(documentInfo.isEncrypted());
New API
Watermarker watermarker = new Watermarker("test.ppt");
IDocumentInfo info = watermarker.getDocumentInfo();
System.out.println("File type: " + info.getFileType());
System.out.println("Number of pages: " + info.getPageCount());
System.out.println("Document size: " + info.getSize() + " bytes");
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.