We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words for password protected documents

Hi,

I am using Aspose.Words to read contents from word documents. I am using
below code to open the password protected documents using Aspose.words.

try

{

Document mainDoc = new Document(m_documentFile);

}

catch (Aspose.Words.IncorrectPasswordException)

{

LoadOptions loadOps = new LoadOptions();

loadOps.Password = “Test”;

mainDoc = new Document(m_documentFile,loadOps);

}

But my current requirement is to loop the list of passwords provided by the
user and try to open the document utile the document opens successfully.

Please provide the sample code for this as soon as possible.

Thanks,

Dhivya<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

Hi Dhivya,

You can use the following code for this purpose.

string
m_documentFile = “PasswordProtected.docx”;<?xml:namespace prefix = “o” ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

Document mainDoc=null;<o:p></o:p>

try<o:p></o:p>

{<o:p></o:p>

mainDoc = new
Document(m_documentFile);<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

catch
(Aspose.Words.IncorrectPasswordException)<o:p></o:p>

{<o:p></o:p>

string[]
passwordsToTry = new string[]
{“Pass1”, “Pass2”,
“testpass”, “Pass3” };<o:p></o:p>

<o:p></o:p>

LoadOptions
loadOps = new LoadOptions();<o:p></o:p>

<o:p> </o:p>

for (int i = 0; i < passwordsToTry.Length; i++)<o:p></o:p>

{<o:p></o:p>

try<o:p></o:p>

{<o:p></o:p>

loadOps.Password =
passwordsToTry[i];<o:p></o:p>

mainDoc = new
Document(m_documentFile, loadOps);<o:p></o:p>

break;<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

catch
(Aspose.Words.IncorrectPasswordException)<o:p></o:p>

{<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

}<o:p></o:p>

//Continue your operations<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(mainDoc);<o:p></o:p>

Best Regards,

Hi,

Is there a way to open password protected document from a stream using Aspose.Words. I am getting "Unsupported file format" exception.

Stream stream = File.OpenRead(MyDir + PasswordProtected.docx");

Document mainDoc=null;

try

{

mainDoc = newDocument(stream);

}

catch(Aspose.Words.IncorrectPasswordException)

{

string[] passwordsToTry = new string[] {"Pass1", "Pass2","testpass", "Pass3" };

LoadOptionsloadOps = new LoadOptions();

for (int i = 0; i < passwordsToTry.Length; i++)

{

try

{

loadOps.Password = passwordsToTry[i];

mainDoc = newDocument(stream, loadOps);

break;

}

catch(Aspose.Words.IncorrectPasswordException)

{

}

}

}

Thanks,

Dhivya

Hi Dhivya,

You can use the following code to read from stream.

string m_documentFile = "PasswordProtected.docx";

MemoryStream ms = new MemoryStream(File.ReadAllBytes(m_documentFile));

<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

Document mainDoc=null;

try

{

mainDoc = new Document(ms);

}

Best Regards,