Password-protected documents

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:

  • PDF: Standard password encryption (user password)
  • Office Documents: Password-protected DOCX, XLSX, PPTX files
  • Legacy Office: Password-protected DOC, XLS, PPT files

Basic Usage

Here are the steps to work with password-protected documents:

  1. Instantiate the LoadOptions object and pass the password in the LoadOptions(String) constructor;
  2. Create Parser object with LoadOptions;
  3. Call any extraction method.

The following code sample shows how to process password-protected documents:

try {
    String password = "123456";
    // Create an instance of Parser class with the password:
    try (Parser parser = new Parser(Constants.SamplePassword, new LoadOptions(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 (TextReader reader = parser.getText()) {
            System.out.println(reader.readToEnd());
        }
    }
} catch (InvalidPasswordException ex) {
    // 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):

ExceptionWhen It’s ThrownResolution
InvalidPasswordExceptionPassword is incorrect, empty, or not providedProvide the correct password in LoadOptions
UnsupportedDocumentFormatExceptionDocument format is not recognized or not supportedCheck if the file format is in the supported formats list
CorruptedDocumentExceptionDocument is corruptedVerify file integrity
GroupDocsParserExceptionGeneral parsing errorCheck the exception message

The following example handles these exceptions; catch the specific exceptions before GroupDocsParserException:

try (Parser parser = new Parser(filePath, new LoadOptions(password))) {
    try (TextReader reader = parser.getText()) {
        System.out.println(reader == null ? "Text extraction isn't supported." : reader.readToEnd());
    }
} catch (InvalidPasswordException ex) {
    // Thrown when password is incorrect or empty
    System.out.println("Invalid password: " + ex.getMessage());
} catch (UnsupportedDocumentFormatException ex) {
    // Thrown when document format is not supported
    System.out.println("Unsupported format: " + ex.getMessage());
} catch (GroupDocsParserException ex) {
    // 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
FileInfo info = 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
    String password = getPasswordFromUser();

    try (Parser parser = new Parser(filePath, new LoadOptions(password))) {
        // Process the document
    }
} else {
    System.out.println("Document is not password-protected.");
    try (Parser parser = new Parser(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

public static void extractTextFromProtectedPdf(String filePath, String password) {
    try {
        // Check if password is needed
        FileInfo fileInfo = Parser.getFileInfo(filePath);

        LoadOptions loadOptions = fileInfo.isEncrypted()
                ? new LoadOptions(password)
                : new LoadOptions();

        try (Parser parser = new Parser(filePath, loadOptions)) {
            if (!parser.getFeatures().isText()) {
                System.out.println("Text extraction is not supported.");
                return;
            }

            try (TextReader reader = parser.getText()) {
                if (reader != null) {
                    String extractedText = reader.readToEnd();
                    System.out.println(String.format("Extracted text length: %d characters", extractedText.length()));
                }
            }
        }
    } catch (InvalidPasswordException ex) {
        System.out.println("Error: Invalid password provided.");
    } catch (Exception ex) {
        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:

Free online document parser App

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

You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.