GroupDocs.Parser provides the functionality to open password-protected documents.
The following are the steps to work with password-protected documents:
Create an instance of the LoadOptions class;
Set the password in the LoadOptions(String) constructor;
Create an instance of the Parser class with these options and call any method.
The following code sample shows how to process password-protected documents:
constjava=require('java');constgroupdocs=require('@groupdocs/groupdocs.parser');try{constpassword='123456';// Create an instance of Parser class with the password
constparser=newgroupdocs.Parser('samplePassword.pdf',newgroupdocs.LoadOptions(password));try{// Check if text extraction is supported
if(!parser.getFeatures().isText()){console.log("Text extraction isn't supported.");}else{// Print the document text
constreader=parser.getText();try{console.log(reader.readToEnd());}finally{reader.close();}}}finally{parser.close();}}catch(err){// The Java exception is available in err.cause
if(err.cause&&java.instanceOf(err.cause,'com.groupdocs.parser.exceptions.InvalidPasswordException')){// Print the message if the password is incorrect or empty
console.log('Invalid password');}else{throwerr;}}process.exit(0);
If the password is incorrect or empty, InvalidPasswordException is thrown. The error thrown in JavaScript wraps the Java exception: check it with java.instanceOf(err.cause, ...).
The following code shows how to check whether a file is password-protected:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Get a file info
constinfo=groupdocs.Parser.getFileInfo('samplePassword.pdf');// Check isEncrypted property
console.log(info.isEncrypted()?'Password is required':'Password is not required');process.exit(0);
More resources
Free online document parser App
Along with the full-featured library we provide simple but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our Free Online Document Parser App.
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
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.