Cannot open Encrypted Ms Word file

We are facing issue in opening MS Word documents which are encrypted through Microsoft Purview settings based on the sensitivity label set on the document. The Purview is configured in a way that the document is not accessible outside the organization. The below error is thrown when trying to open document using

Document doc = new Document(fileName);
 com.aspose.words.UnsupportedFileFormatException: Unknown file format.
	at com.aspose.words.DigitalSignatureUtil.zzXkR(Unknown Source)
	at com.aspose.words.zzWqK.zzYp1(Unknown Source)
	at com.aspose.words.Document.zzYRr(Unknown Source)
	at com.aspose.words.Document.zzXkR(Unknown Source)
	at com.aspose.words.Document.<init>(Unknown Source)
	at com.aspose.words.Document.<init>(Unknown Source)

We are using Aspose 23.11
Please let us know if Aspose is capable of opening such documents.

@yalshaiba

It appears that you are encountering an issue when trying to open Microsoft Word documents that are encrypted using Microsoft Purview settings. The error message you are receiving indicates that the document format is unknown, which typically occurs when the document is encrypted and requires a password to access.

Aspose.Words can open encrypted documents, but you must provide the correct password. If the document is encrypted and you do not have the password, it will not be possible to open the document using Aspose.Words, and you will encounter an exception indicating that the document is encrypted.

To check if a document is encrypted before attempting to open it, you can use the FileFormatUtil.DetectFileFormat method, which will return a FileFormatInfo object. This object contains a property called IsEncrypted that indicates whether the document is encrypted. Here’s a code example demonstrating how to check for encryption:

using Aspose.Words;
using Aspose.Words.Util;

string fileName = "path_to_your_document.docx";
FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);

if (info.IsEncrypted)
{
    Console.WriteLine("The document is encrypted. Please provide the password to open it.");
}
else
{
    Document doc = new Document(fileName);
    // Proceed with your operations on the document
}

If you have the password, you can open the document like this:

string password = "your_password";
Document doc = new Document(fileName, new LoadOptions(password));

If you do not have the password, unfortunately, you will not be able to access the content of the document.

For more detailed information on handling encrypted documents with Aspose.Words, you can refer to the official documentation on document encryption and protection.

If you have any further questions or need additional assistance, feel free to ask!

Sources:
[1]: Encrypt a Document in C#|Aspose.Words for .NET - Aspose Documentation
[2]: Protect or Encrypt a Document in C#|Aspose.Words for .NET

@yalshaiba You should specify password in document load options to open encrypted document:

LoadOptions opt = new LoadOptions();
opt.setPassword("mypass");
Document doc = new Document("in.docx", opt);