Password Protected PDF documents

Hi,

I am using Aspose.Pdf product to open the password protected pdf documents. But my current requirement is to loop the list of passwords until the document open with correct password. please provide me the same code for this.

PdfFileInfopdfFileinfo = null;

     try

{

pdfFileinfo = new PdfFileInfo(stream);

}

catch (Exception ex)

{

throw new Exception(string.Format("Unable to open the PDF file.\n: {0}", ex.Message));

}

     if (pdfFileinfo.IsEncrypted)
     </p><p>{
         </p><p>pdfFileinfo = new PdfFileInfo(stream, "Password-1");
                              </p><p>}</p><p>Thanks,</p><p>Dhivya</p>

Hi Dhivya,

Thanks for contacting support and sorry for the delayed response.

Please try using the following code snippet to iterate through the array of passwords and determine the correct password.

C#

// load source PDF file
PdfFileInfo info = new PdfFileInfo();
info.BindPdf("c:/pdftest/Lorem+Ipsum_protected.pdf");

// determine if the source PDF is
encrypted
Console.WriteLine("File is password protected " + info.IsEncrypted);

String[] passwords = new String[5]{"test","test1","test2","test3","sample"};
for (int passwordcount = 0;
passwordcount < passwords.Length; passwordcount++)
{
    try
    {
        Document doc = new Document("c:/pdftest/Lorem+Ipsum_protected.pdf",
passwords[passwordcount]);
        if
(doc.Pages.Count>0)
            Console.WriteLine("Number of Page in document are = " +doc.Pages.Count);
    }
    catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
    {
        Console.WriteLine("Password = " +passwords[passwordcount]+ " is not correct");
    }
}

Hi,

I am facing the “Cannot access closed file” exception in the following scenario.

private void GetDocumentPropertiesFromFile()
{
    using (FileStream fs = File.OpenRead("c:/pdftest/Lorem+Ipsum_protected.pdf"))
    {
        ExtractMetadata((Stream)fs);
    }

    private void ExtractMetadata(Stream stream)
    {
        PdfFileInfo pdfFileinfo = null;
        try
        {
            pdfFileinfo = new PdfFileInfo(stream);
        }
        catch (Exception ex)
        {
            throw new Exception($"Unable to open the PDF file.\n<Error>: {ex.Message}", ex);
        }

        if (pdfFileinfo.IsEncrypted)
        {
            OpenDocument(stream);
        }
    }

    private void OpenDocument(Stream fileStream)
    {
        Document doc = null;
        String[] passwords = new String[5]{"test","test1","test2","test3","sample"};
        for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
        {
            try
            {
                fileStream.Position = 0;
                doc = new Document(fileStream, passwords[passwordcount]);
                break;
            }
            catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
            {
                continue;
            }
        }
    }

The above code works fine for the first iteration, i.e., passwordcount = 0. If that password is not correct, then passwordcount will increment to 1. At that time, I am facing the “Cannot access a closed file.” Exception. Please let me know the root cause of this issue.

Thanks,
Dhivya

Hi Dhivya,

I have tested the scenario and I am able to reproduce the same problem. For the sake of correction, I have logged it in our issue tracking system as PDFNEWNET-36777. We will investigate this issue in detail and will keep you updated on the status of a correction.

We apologize for your inconvenience.

Hi Dhivya,

Thanks for your patience. Please review the following code snippet that iterates through a password loop. It should help you accomplish the task.

private void Foo()
{
    using (FileStream fs = File.OpenRead(@"c:/pdftest/Encrypted.pdf"))
    {
        ExtractMetadata(fs);
    }

    private void ExtractMetadata(Stream stream)
    {
        String[] passwords = new String[] { "test", "test1", "test2", "test3", "sample" };

        for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
        {
            try
            {
                stream.Seek(0, SeekOrigin.Begin);
                using (Document document = new Document(stream, passwords[passwordcount], false))
                {
                    if (document.Metadata.Count > 0)
                    {
                        // Do smth
                    }
                }
            }
            catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
            {
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Unable to open the PDF file.\n<Error>: {0}", ex.Message));
            }
        }
    }
}

Please feel free to contact us for any further assistance.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36777) have been fixed in Aspose.Pdf for .NET 9.2.0.

The blog post for this release is created over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi,

Thank you for your sample code.

Regards,

Dhivya

Hi Dhivya,


Thanks for your feedback. It is good to know that sample code snippet helped you to accomplish your requirements.

Please feel free to contact us for any further assistance, we will be more than happy to extend our support.

Best Regards,