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

Free Support Forum - aspose.com

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<o:p></o:p>

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 at the below acenario.

private void GetDocumentPropertiesFromFile()

{

vusing (FileStream fs = File.OpenRead(“c:/pdftest/Lorem+Ipsum_protected.pdf”))<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

ExtractMetadata((Stream)fs);

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

private void ExtractMetadata(Stream stream)<o:p></o:p>

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

PdfFileInfo pdfFileinfo = null;<o:p></o:p>

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

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

pdfFileinfo = new PdfFileInfo(stream);<o:p></o:p>

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

catch (Exception ex)<o:p></o:p>

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

throw new Exception(string.Format(“Unable to
open the PDF file.\n: {0}”
,
ex.Message));<o:p></o:p>

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

if (pdfFileinfo.IsEncrypted)<o:p></o:p>

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

OpenDocument(stream);<o:p></o:p>

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

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

private void OpenDocument(Stream fileStream)<o:p></o:p>

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

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

String[]
passwords = new String[5]{“test”,“test1”,“test2”,“test3”,“sample”};<o:p></o:p>

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

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

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

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

fileStream.Position = 0;<o:p></o:p>

doc = new Document(fileStream, passwords[passwordcount]);<o:p></o:p>

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

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

catch (Aspose.Pdf.Exceptions.InvalidPasswordException)<o:p></o:p>

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

continue;<o:p></o:p>

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

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

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

The above code is working
fine for 1st iteration. I mean passwordcount =0 working fine. If that
password is not correct, then the passwordcount will get increment to 1. That time
I am facing “Cannot access a closed file.” Exception. Please let me know the route
cause for this issue.
<o:p></o:p>

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 details and will keep you updated on the status of a
correction. <o:p></o:p>

We apologize for your inconvenience.

Hi Dhivya,


Thanks for your patience. Please have a look into following code snippet to iterate through password loop. Hopefully it will help you to accomplish the task.

private void Foo()<o:p></o:p>

{

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: {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,