lokesh
lokesh

Reputation: 85

PDFsharp System Out of Memory Exception

I have a fairly large PDF document (more than 1 GB) but I am only concerned with the first page of it. I was doing the following in order to avoid taking complete PDF in memory.

PdfPage inputpdf = PdfReader.Open(filepath).Pages[0];

But even this is not working and I am getting system out of memory exception every other time. Anyone have any idea how to work with large file without storing the complete information in memory. All I am looking for is one page.

Below is stack trace.

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at PdfSharp.Pdf.IO.Lexer.ReadStream(Int32 length)
   at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream)
   at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
   at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)

P.S. I am working on .NET Core, Azure function EP1 model. (3.5 GB memory)

Upvotes: 0

Views: 1439

Answers (1)

PDFsharp reads the whole document when you call PdfReader.Open() and appending .Pages[0] does not reduce the required memory at all.

If your code runs in 32 bit mode then try switching to 64 bit mode to break the 2 GB barrier.

If that does not help then consider upgrading your Azure model to include more memory or maybe try a different tool other than PDFsharp.

Upvotes: 1

Related Questions