I have a good one for you. I am using ASPOSE.PDF (v 6.3) to extract pages out of an existing PDF and create new, separate PDF documents from a large existing PDF document. The existing PDF has bookmarks that I use to determine when to create new PDF files and which pages to use. This has been working fine for a little over a month. Yesterday my client finally found a way to break it by adding in PDF files that, I think, are too large. This particular PDF file that I am parsing is a 250 MB PDF file. I get errors in a couple locations; and it isn’t consistent.
I try running this document through my dev system and the first time I hit the error was loading the document:
private Document m_pdfDocument;
m_pdfDocument = new Document(ExhibitFile);
“ExhibitFile” is a string to the path of the PDF document. The first time I ran a test I get the error “Exception of type ‘System.OutOfMemoryException’ was thrown.” on the second line of code setting the Document = new Document.
So I stopped the debugger, took some notes, did a few things then tried again. The second time I got the same error but on a different line of code. The second time it actually loaded the document, but trhew the error as I was adding the pages to the new document.
private string ExtractSectionF(int startPage, int endPage)
{
Document newDocument = new Document();
string filePath = string.Empty;
string fileName = string.Empty;
//loop through all the pages
foreach (Page pdfPage in m_pdfDocument.Pages)
{
if ((pdfPage.Number >= startPage) && (pdfPage.Number <= endPage))
{
newDocument.Pages.Add(pdfPage);
}
}
fileName = string.Format("\{0}.pdf", GetUniqueKey());
// need to generate a unique file name
filePath = WorkingDirectory + fileName;
newDocument.Save(filePath);
return filePath;
}
In the above case startPage = 3513, endPage = 3812, and pdfPage.Number = 3670. The error occurs adding the page to the newDocument.
So here is my question; should I be handling larger PDF files differently? Is there a better way to code this to handle memory more efficiently?
Any input is appreciated.