GroupDocs.Parser provides the functionality to open and process password-protected documents including encrypted PDFs and password-protected Office files (DOCX, XLSX, PPTX).
Supported Encryption Types
The library supports the following encryption types:
The following code sample shows how to process password-protected documents:
try{Stringpassword="123456";// Create an instance of Parser class with the password:
try(Parserparser=newParser(Constants.SamplePassword,newLoadOptions(password))){// Check if text extraction is supported
if(!parser.getFeatures().isText()){System.out.println("Text extraction isn't supported.");return;}// Print the document text
try(TextReaderreader=parser.getText()){System.out.println(reader.readToEnd());}}}catch(InvalidPasswordExceptionex){// Print the message if the password is incorrect or empty
System.out.println("Invalid password");}
Exception Handling
When working with password-protected documents, the following exceptions may be thrown. All of them extend GroupDocsParserException, which is an unchecked exception (RuntimeException):
The following example handles these exceptions; catch the specific exceptions before GroupDocsParserException:
try(Parserparser=newParser(filePath,newLoadOptions(password))){try(TextReaderreader=parser.getText()){System.out.println(reader==null?"Text extraction isn't supported.":reader.readToEnd());}}catch(InvalidPasswordExceptionex){// Thrown when password is incorrect or empty
System.out.println("Invalid password: "+ex.getMessage());}catch(UnsupportedDocumentFormatExceptionex){// Thrown when document format is not supported
System.out.println("Unsupported format: "+ex.getMessage());}catch(GroupDocsParserExceptionex){// General parsing exception
System.out.println("Parsing error: "+ex.getMessage());}
Check if Document is Password-Protected
Before attempting to open a document, you can check if it requires a password. The getFileInfo static method of Parser class returns a FileInfo object, and its isEncrypted method returns true if a password is required. getFileInfo accepts a file path or an InputStream:
// Get a file info
FileInfoinfo=Parser.getFileInfo(filePath);// Check if the document is encrypted/password-protected
if(info.isEncrypted()){System.out.println("This document is password-protected. Password is required.");// Prompt user for password or load it from configuration
Stringpassword=getPasswordFromUser();try(Parserparser=newParser(filePath,newLoadOptions(password))){// Process the document
}}else{System.out.println("Document is not password-protected.");try(Parserparser=newParser(filePath)){// Process the document without password
}}
Note
For a password-protected DOCX, XLSX or PPTX file, getFileType returns FileType.Unknown, because the format of an encrypted document cannot be read without the password. getSize returns the file size. For PDF documents isEncrypted() returns true only for encrypted documents. Both behaviours apply since version 26.10.
Complete Example: Extract Text from Password-Protected PDF
publicstaticvoidextractTextFromProtectedPdf(StringfilePath,Stringpassword){try{// Check if password is needed
FileInfofileInfo=Parser.getFileInfo(filePath);LoadOptionsloadOptions=fileInfo.isEncrypted()?newLoadOptions(password):newLoadOptions();try(Parserparser=newParser(filePath,loadOptions)){if(!parser.getFeatures().isText()){System.out.println("Text extraction is not supported.");return;}try(TextReaderreader=parser.getText()){if(reader!=null){StringextractedText=reader.readToEnd();System.out.println(String.format("Extracted text length: %d characters",extractedText.length()));}}}}catch(InvalidPasswordExceptionex){System.out.println("Error: Invalid password provided.");}catch(Exceptionex){System.out.println("Error: "+ex.getMessage());}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: