Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active February 7, 2022 10:52
Show Gist options
  • Save GroupDocsGists/7896b4529c25d419b78b95469e8fe257 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/7896b4529c25d419b78b95469e8fe257 to your computer and use it in GitHub Desktop.
Password Protection of Word Files in Java by Adding, Removing, and Changing Passwords
/*
* Password Protect Word Documents in Java
*/
AddPasswordOptions addOptions = new AddPasswordOptions("mySECRETpassWORD");
Merger merger = new Merger("path/document.docx");
merger.addPassword(addOptions);
merger.save("path/protected-document.docx");
/*
* Change password of the protected DOC/DOCX documents in Java
*/
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD");
UpdatePasswordOptions updateOptions = new UpdatePasswordOptions("TOPSECRET_pa22WORD");
Merger merger = new Merger("path/protected-document.docx", loadOptions);
merger.updatePassword(updateOptions);
merger.save("path/pwd-changed-document.docx");
/*
* Remove password from Word document in Java
*/
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD");
Merger merger = new Merger("path/protected-document.docx", loadOptions);
merger.removePassword();
merger.save("path/no-pwd-document.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment