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");}
The following code shows how to check whether a file is password-protected:
// Get a file info
FileInfoinfo=Parser.getFileInfo(filePath);// Check IsEncrypted property
System.out.println(info.isEncrypted()?"Password is required":"");
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: