0

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)

2
  • Is the code running reliably on your developer machine? Commented Mar 11, 2020 at 17:14
  • Yes it working as expected in my machine. Commented Mar 11, 2020 at 17:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Suggestion for any other tool. Can this be done through iTextsharp or spirepdf?
@lokesh We use PDF4Net which does let you change a page at a time. You could try an evaluation version to see if it does what you want before spending any money.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.