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
Warning
Note: The library cannot decrypt documents with owner passwords (restrictions passwords) that only restrict printing/editing. Only documents with user passwords (opening passwords) are supported.

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 with comprehensive error handling:

using GroupDocs.Parser;
using GroupDocs.Parser.Options;
using GroupDocs.Parser.Exceptions;
using System;

try
{
    string password = "your-password";
    
    // Create LoadOptions with password
    LoadOptions loadOptions = new LoadOptions(password);
    
    // Create an instance of Parser class with the password
    using (Parser parser = new Parser(filePath, loadOptions))
    {
        // Check if text extraction is supported
        if (!parser.Features.Text)
        {
            Console.WriteLine("Text extraction isn't supported for this format.");
            return;
        }
        
        // Print the document text
        using (TextReader reader = parser.GetText())
        {
            if (reader != null)
            {
                Console.WriteLine(reader.ReadToEnd());
            }
            else
            {
                Console.WriteLine("Text extraction returned null.");
            }
        }
    }
}
catch (InvalidPasswordException ex)
{
    // Thrown when password is incorrect or empty
    Console.WriteLine($"Invalid password: {ex.Message}");
}
catch (UnsupportedDocumentFormatException ex)
{
    // Thrown when document format is not supported
    Console.WriteLine($"Unsupported format: {ex.Message}");
}
catch (ParserException ex)
{
    // General parsing exception
    Console.WriteLine($"Parsing error: {ex.Message}");
}

Exception Handling

When working with password-protected documents, the following exceptions may be thrown:

ExceptionWhen It’s ThrownResolution
InvalidPasswordExceptionPassword is incorrect, empty, or not providedProvide the correct password in LoadOptions
UnsupportedDocumentFormatExceptionDocument format is not supportedCheck if the file format is in the supported formats list
ParserExceptionGeneral parsing error (corrupted file, insufficient permissions, etc.)Verify file integrity and access permissions

Check if Document is Password-Protected

Before attempting to open a document, you can check if it requires a password:

using GroupDocs.Parser.Options;

// Get file information
FileInfo info = Parser.GetFileInfo(document);

// Check if the document is encrypted/password-protected
if (info.IsEncrypted)
{
    Console.WriteLine("This document is password-protected. Password is required.");
    // Prompt user for password or load from configuration
    string password = GetPasswordFromUser();
    
    LoadOptions loadOptions = new LoadOptions(password);
    using (Parser parser = new Parser(document, loadOptions))
    {
        // Process the document
    }
}
else
{
    Console.WriteLine("Document is not password-protected.");
    using (Parser parser = new Parser(document))
    {
        // Process the document without password
    }
}

Complete Example: Extract Text from Password-Protected PDF

using GroupDocs.Parser;
using GroupDocs.Parser.Options;
using GroupDocs.Parser.Exceptions;

public 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();
        
        using (Parser parser = new Parser(filePath, loadOptions))
        {
            if (!parser.Features.Text)
            {
                Console.WriteLine("Text extraction is not supported.");
                return;
            }
            
            using (TextReader reader = parser.GetText())
            {
                if (reader != null)
                {
                    string extractedText = reader.ReadToEnd();
                    Console.WriteLine($"Extracted text length: {extractedText.Length} characters");
                }
            }
        }
    }
    catch (InvalidPasswordException)
    {
        Console.WriteLine("Error: Invalid password provided.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

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 .NET 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 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.