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