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

Free Support Forum - aspose.com

Converting Password Protected Files

We have a need to convert password protected Word and Excel files. Is there a way for Aspose to detect whether the file has a password and handle this? Any ideas of what other folks have done in this situation?

Hi Jeanette,

Yes, you can convert encrypted Word and Excel documents. You need to supply a valid password for encrypted documents otherwise you will get incorrect password exception.

You can catch incorrect password exception to confirm if document was protected and also use it to try multiple passwords e.g.

string
m_documentFile = “Input.docx”;

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

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

Document mainDoc = null;

try

{

LoadOptions
loadOps = new LoadOptions();

<o:p> </o:p>

loadOps.Password = “ranjan”;

mainDoc = new
Document(m_documentFile, loadOps);

}

<o:p> </o:p>

catch
(Aspose.Words.IncorrectPasswordException)

{

string[]
passwordsToTry = new string[]
{ “Pass1”, “Pass2”, “Pass3”
};

<o:p> </o:p>

LoadOptions
loadOps = new LoadOptions();

<o:p> </o:p>

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

{

try

{

loadOps.Password =
passwordsToTry[i];

mainDoc = new
Document(m_documentFile, loadOps);

break;

}

<o:p> </o:p>

catch
(Aspose.Words.IncorrectPasswordException)

{

}

}

<o:p> </o:p>

}

Best Regards,

Thanks for the info. I am not the programmer so I apologize for my additional questions. The files are being uploaded by our customers. Could we detect if the document is password protected and then prompt the user for the password?

Hi Jeanette,

You can use FileFormatInfo.IsEncrypted to check if the file is password protected. Same class and property is available in both Aspose.Words and Aspose.Cells namespaces. Please check How to detect if a file is encrypted for more details.

Best Regards,