Free Support Forum - aspose.com

Aspose PDF not getting exception for corrupted pdf files

Hi,


I am tryinf concatenate all the memory steams into single pdf, But If there is any one file is correpted all thye files are not concatenating.I am getting 0 KB.How to catch the corrupted file and skip that file.

Please help me on this ASAP.
Here is my code .
//create two file streams to read pdf files
FileStream fs1 = new FileStream(@“C:\Siddarth\P07\text-Copy.pdf”, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(@“C:\Siddarth\P07\text-Copy.pdf”, FileMode.Open, FileAccess.Read);

//create byte arrays to keep the contents of PDF files
byte[] buffer1 = new byte[Convert.ToInt32(fs1.Length)];
byte[] buffer2 = new byte[Convert.ToInt32(fs2.Length)];


int i = 0;
//read PDF file contents into byte arrays
i = fs1.Read(buffer1, 0, Convert.ToInt32(fs1.Length));
i = fs2.Read(buffer2, 0, Convert.ToInt32(fs2.Length));

//now, first convert byte arrays into MemoryStreams and then concatenate those streams
using (MemoryStream pdfStream = new MemoryStream())
{
using (MemoryStream fileStream1 = new MemoryStream(buffer1))
{
using (MemoryStream fileStream2 = new MemoryStream(buffer2))
{
//create instance of PdfFileEditor class to concatenate streams
PdfFileEditor pdfEditor = new PdfFileEditor();
//concatenate both input MemoryStreams and save to putput MemoryStream
pdfEditor.Concatenate(fileStream1, fileStream2, pdfStream);
//convert MemoryStream back to byte array
byte[] data = pdfStream.ToArray();
//create a FileStream to save the output PDF file
FileStream output = new FileStream(@“C:\Siddarth\P07\merged_output.pdf”, FileMode.Create,
FileAccess.Write);
//write byte array contents in the output file stream
output.Write(data, 0, data.Length);
//close output file
output.Close();
}
}
}
//close input files
fs1.Close();
fs2.Close();


In the above code I have multiple streams.While concatenate i am getting 0 KB in out put.
I just came to know that there is one pdf file got corrupted.How to catch corrupted file and skip rees of the memory streams.

Thanks
Siddartha

Hi Siddartha,


Thanks for your inquiry. I have tested the scenario with Aspose.Pdf for .NET 10.7.0 and it is throwing exception while concatenating your shared document. Please download and try latest version of Aspose.Pdf for .NET, it will resolve the issue.

Moreover, you may use following simple function to check whether input PDF document is corrupt or not, before starting any processing and proceed accordingly. Hopefully it will help you to overcome the issue.

bool IsCorrupt(string path)<o:p></o:p>

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

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

{ Document doc = new Document(path); }<o:p></o:p>

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

{ return true; }<o:p></o:p>

return false;<o:p></o:p>

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

<o:p> </o:p>

<o:p>Please feel free to contact us for any further assistance.</o:p>

<o:p>
</o:p>

<o:p>Best Regards,</o:p>

Hi Siddartha,


Adding more Tilal’s comments, I have tried viewing the earlier shared text-Copy.pdf file in Adobe Reader 11.0.3 and Forxit Reader 7.1 and have encountered an error when trying to view the document. It appears that PDF file is not correct or its broken.