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>
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");
}
}
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.
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.