importcom.groupdocs.watermark.Watermarker;importcom.groupdocs.watermark.common.HorizontalAlignment;importcom.groupdocs.watermark.common.VerticalAlignment;importcom.groupdocs.watermark.watermarks.Color;importcom.groupdocs.watermark.watermarks.Font;importcom.groupdocs.watermark.watermarks.FontStyle;importcom.groupdocs.watermark.watermarks.TextWatermark;publicclassHelloWorld{publicstaticvoidmain(String[]args){// Specify an absolute or relative path to your document.
try(Watermarkerwatermarker=newWatermarker("C:\\Docs\\sample.docx")){// Specify the desired text and font for the watermark
TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Arial",36,FontStyle.Bold|FontStyle.Italic));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setOpacity(0.4);watermark.setRotateAngle(45);watermark.setForegroundColor(Color.getRed());// Apply the watermark
watermarker.add(watermark);// Save the resulting document
watermarker.save("C:\\Docs\\watermarked-sample.docx");}}}
Watermarker implements java.io.Closeable, so the try-with-resources statement closes it for you.
Additionally, if you are certain about your file extension or know the document type in advance, you can specify the file type through the LoadOptions class. Specifying it eliminates the need for format detection, enabling faster and more efficient document processing:
importcom.groupdocs.watermark.Watermarker;importcom.groupdocs.watermark.common.FileType;importcom.groupdocs.watermark.common.FormatFamily;importcom.groupdocs.watermark.options.LoadOptions;StringfilePath="C:\\Docs\\sample.docx";LoadOptionsloadOptions=newLoadOptions();// FileType.fromExtension expects the extension with the period, for example ".docx"
loadOptions.setFileType(FileType.fromExtension(filePath.substring(filePath.lastIndexOf('.'))));// Or set the format family directly when you load a document from a stream, for example:
// loadOptions.setFormatFamily(FormatFamily.WordProcessing);
try(Watermarkerwatermarker=newWatermarker(filePath,loadOptions)){// ....
}
Running
Run the program. A new watermarked document will appear in the specified path.
What’s next
Congratulations! You have added a simple text watermark to a document. Read the Developer guide and API reference to learn how to customize text watermarks, add images as watermarks, search documents for existing watermarks, and much more.
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.
On this page
Ask AI
Ask anything about this page — the answer is based on its content.
Searching the docs and writing an answer…
Something went wrong while getting an answer. Please try again in a moment.