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));

Document mainDoc = null;
try
{
    LoadOptions loadOps = new LoadOptions();

    loadOps.Password = "ranjan";
    mainDoc = new Document(m_documentFile, loadOps);
}

catch (Aspose.Words.IncorrectPasswordException)
{
    string[] passwordsToTry = new string[] { "Pass1", "Pass2", "Pass3" };

    LoadOptions loadOps = new LoadOptions();

    for (int i = 0; i < passwordsToTry.Length; i++)
    {
        try
        {
            loadOps.Password = passwordsToTry[i];
            mainDoc = new Document(m_documentFile, loadOps);
            break;
        }
        catch (Aspose.Words.IncorrectPasswordException)
        {
        }
    }
}

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,