Load password-protected document

Some document formats also support content encryption. To load these type of documents you will have to provide the password. GroupDocs.Watermark API allows you to load content of these documents to manage watermark.

Load password-protected document of any supported format

The following example demonstrates how to load an encrypted document of any supported format using the password. If the password is incorrect, InvalidPasswordException is thrown.

advanced_usage.loading_documents.LoadPasswordProtectedDocument

LoadOptions loadOptions = new LoadOptions();                                                                                   
loadOptions.setPassword("P@$$w0rd");                                                                                           
// Constants.InProtectedDocumentDocx is an absolute or relative path to your document. Ex: @"C:\\Docs\\protected-document.docx"
String filePath = Constants.InProtectedDocumentDocx;                                                                           
Watermarker watermarker = new Watermarker(filePath, loadOptions);                                                              
                                                                                                                               
// use watermarker methods to manage watermarks in the document                                                                
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 12));                                          
                                                                                                                               
watermarker.add(watermark);                                                                                                    
                                                                                                                               
watermarker.save(Constants.OutProtectedDocumentDocx);                                                                          
                                                                                                                               
watermarker.close();                                                                                                         

Load password-protected word processing document

The following example demontrates how to load an encrypted word processing document (DOC, DOCX etc) using the password.

advanced_usage.loading_documents.LoadPasswordProtectedWordProcessingDocument

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                                       
loadOptions.setPassword("P@$$w0rd");                                                                                           
// Constants.InProtectedDocumentDocx is an absolute or relative path to your document. Ex: @"C:\\Docs\\protected-document.docx"
String filePath = Constants.InProtectedDocumentDocx;                                                                           
Watermarker watermarker = new Watermarker(filePath, loadOptions);                                                              
                                                                                                                               
// use watermarker methods to manage watermarks in the WordProcessing document                                                 
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 12));                                          
                                                                                                                               
watermarker.add(watermark);                                                                                                    
                                                                                                                               
watermarker.save(Constants.OutProtectedDocumentDocx);                                                                          
                                                                                                                               
watermarker.close();                                                                                                         

The following LoadOptions descendants use setPassword method:

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.