OutOfMemoryException when opening a valid ~1 GiB PDF in PdfReader.Open()
Description
I encountered an OutOfMemoryException when opening a valid PDF file of approximately 1 GiB with PDFsharp 6.2.4.
The PDF can be opened normally by common PDF readers.
Interestingly, I also tested PDFsharp with a real multi-page PDF of approximately 13 GiB, and PDFsharp was able to open and merge that file successfully (although it was understandably slow). Therefore, this does not appear to be a simple file-size limitation.
The issue seems to be related to a particular PDF structure and occurs while PDFsharp is parsing the trailer.
Environment
- PDFsharp: 6.2.4
- .NET: 8.0
- OS: Windows 11
- x64
Reproduction
The exception occurs with:
using var document =
PdfReader.Open(filePath, PdfDocumentOpenMode.Import);
The file is approximately 1 GiB in size.
Exception
System.OutOfMemoryException
HResult=0x8007000E
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Stack trace:
System.String.Ctor(Char[] value)
System.Text.Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
PdfSharp.Pdf.IO.Lexer.ScanRawString(Int64 position, Int32 length)
PdfSharp.Pdf.IO.Parser.ReadTrailer()
PdfSharp.Pdf.IO.PdfReader.OpenFromStream(...)
PdfSharp.Pdf.IO.PdfReader.OpenFromFile(...)
PdfSharp.Pdf.IO.PdfReader.Open(...)
PdfFormatDetector.Detect(...)
The relevant part appears to be:
Parser.ReadTrailer()
-> Lexer.ScanRawString(...)
-> Encoding.GetString(...)
-> OutOfMemoryException
This suggests that ScanRawString() is attempting to materialize a very large section of the PDF as a .NET string.
Additional tests
I performed several tests to determine whether this was simply caused by the file size.
1. The original ~1 GiB PDF
- Opens successfully in common PDF readers.
PdfReader.Open(..., Import) in PDFsharp 6.2.4 throws OutOfMemoryException.
2. Re-saving the PDF
I opened the PDF in another PDF application and used "Save As" to save it again.
The resulting PDF was still around 1 GiB and PDFsharp still threw the same OutOfMemoryException.
3. Printing the PDF to another PDF
I printed the document using a PDF printer, which regenerated the PDF.
The resulting file was only approximately 536 KB.
PDFsharp could open the regenerated PDF successfully.
4. A real ~13 GiB PDF
I also tested a real multi-page PDF of approximately 13 GiB.
PDFsharp was able to open it and successfully merge it. It was slow, but it did not run out of memory.
This makes me think the problem is related to a particular PDF structure rather than the absolute file size.
Expected behavior
PdfReader.Open() should either:
- successfully open the PDF without allocating an unreasonable amount of memory, or
- fail with a meaningful PDF parsing/compatibility exception if the PDF contains a structure that PDFsharp cannot process.
It would be preferable to avoid attempting to create a huge .NET string from an unexpectedly large range of the input file.
Question
Could this be related to how Parser.ReadTrailer() determines the range passed to Lexer.ScanRawString()?
The stack trace suggests that a very large range is eventually passed to:
Encoding.GetString(byte[], int, int)
which causes the large string allocation and the OutOfMemoryException.
I can provide the problematic PDF if it would help with reproducing and diagnosing the issue.
OutOfMemoryException when opening a valid ~1 GiB PDF in PdfReader.Open()
Description
I encountered an
OutOfMemoryExceptionwhen opening a valid PDF file of approximately 1 GiB with PDFsharp 6.2.4.The PDF can be opened normally by common PDF readers.
Interestingly, I also tested PDFsharp with a real multi-page PDF of approximately 13 GiB, and PDFsharp was able to open and merge that file successfully (although it was understandably slow). Therefore, this does not appear to be a simple file-size limitation.
The issue seems to be related to a particular PDF structure and occurs while PDFsharp is parsing the trailer.
Environment
Reproduction
The exception occurs with:
The file is approximately 1 GiB in size.
Exception
The relevant part appears to be:
This suggests that
ScanRawString()is attempting to materialize a very large section of the PDF as a .NETstring.Additional tests
I performed several tests to determine whether this was simply caused by the file size.
1. The original ~1 GiB PDF
PdfReader.Open(..., Import)in PDFsharp 6.2.4 throwsOutOfMemoryException.2. Re-saving the PDF
I opened the PDF in another PDF application and used "Save As" to save it again.
The resulting PDF was still around 1 GiB and PDFsharp still threw the same
OutOfMemoryException.3. Printing the PDF to another PDF
I printed the document using a PDF printer, which regenerated the PDF.
The resulting file was only approximately 536 KB.
PDFsharp could open the regenerated PDF successfully.
4. A real ~13 GiB PDF
I also tested a real multi-page PDF of approximately 13 GiB.
PDFsharp was able to open it and successfully merge it. It was slow, but it did not run out of memory.
This makes me think the problem is related to a particular PDF structure rather than the absolute file size.
Expected behavior
PdfReader.Open()should either:It would be preferable to avoid attempting to create a huge .NET
stringfrom an unexpectedly large range of the input file.Question
Could this be related to how
Parser.ReadTrailer()determines the range passed toLexer.ScanRawString()?The stack trace suggests that a very large range is eventually passed to:
which causes the large string allocation and the
OutOfMemoryException.I can provide the problematic PDF if it would help with reproducing and diagnosing the issue.