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:
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:
The following code sample shows how to process password-protected documents with comprehensive error handling:
usingGroupDocs.Parser;usingGroupDocs.Parser.Options;usingGroupDocs.Parser.Exceptions;usingSystem;try{stringpassword="your-password";// Create LoadOptions with passwordLoadOptionsloadOptions=newLoadOptions(password);// Create an instance of Parser class with the passwordusing(Parserparser=newParser(filePath,loadOptions)){// Check if text extraction is supportedif(!parser.Features.Text){Console.WriteLine("Text extraction isn't supported for this format.");return;}// Print the document textusing(TextReaderreader=parser.GetText()){if(reader!=null){Console.WriteLine(reader.ReadToEnd());}else{Console.WriteLine("Text extraction returned null.");}}}}catch(InvalidPasswordExceptionex){// Thrown when password is incorrect or emptyConsole.WriteLine($"Invalid password: {ex.Message}");}catch(UnsupportedDocumentFormatExceptionex){// Thrown when document format is not supportedConsole.WriteLine($"Unsupported format: {ex.Message}");}catch(ParserExceptionex){// General parsing exceptionConsole.WriteLine($"Parsing error: {ex.Message}");}
Exception Handling
When working with password-protected documents, the following exceptions may be thrown:
General 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:
usingGroupDocs.Parser.Options;// Get file informationFileInfoinfo=Parser.GetFileInfo(document);// Check if the document is encrypted/password-protectedif(info.IsEncrypted){Console.WriteLine("This document is password-protected. Password is required.");// Prompt user for password or load from configurationstringpassword=GetPasswordFromUser();LoadOptionsloadOptions=newLoadOptions(password);using(Parserparser=newParser(document,loadOptions)){// Process the document}}else{Console.WriteLine("Document is not password-protected.");using(Parserparser=newParser(document)){// Process the document without password}}
Complete Example: Extract Text from Password-Protected PDF
usingGroupDocs.Parser;usingGroupDocs.Parser.Options;usingGroupDocs.Parser.Exceptions;publicvoidExtractTextFromProtectedPdf(stringfilePath,stringpassword){try{// Check if password is neededFileInfofileInfo=Parser.GetFileInfo(filePath);LoadOptionsloadOptions=fileInfo.IsEncrypted?newLoadOptions(password):newLoadOptions();using(Parserparser=newParser(filePath,loadOptions)){if(!parser.Features.Text){Console.WriteLine("Text extraction is not supported.");return;}using(TextReaderreader=parser.GetText()){if(reader!=null){stringextractedText=reader.ReadToEnd();Console.WriteLine($"Extracted text length: {extractedText.Length} characters");}}}}catch(InvalidPasswordException){Console.WriteLine("Error: Invalid password provided.");}catch(Exceptionex){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:
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.
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.