Support opening PDFs larger than 2 GiB - #1293
Merged
Merged
Conversation
added 2 commits
May 14, 2026 19:31
- Introduced a new test in `LocalTests.cs` for creating and reading PDFs larger than two gigabytes using `LargePdfTestDocumentGenerator`. - Updated `FileHeaderOffset.cs` to change the `Value` type from `int` to `long` for better handling of large values. - Modified `PdfDocumentFactory.cs` to use `FileStream` for opening PDFs, enhancing memory efficiency. - Added `PdfDocumentOpenTests` class to verify opening of large PDFs with a sparse XRef offset.
Introduced `RealBigFileTests` to test creation and reading of PDFs larger than 2 GiB. Updated `IntegrationDocumentTests` with a new test for opening large sparse documents and added utility methods for document creation. Removed `LocalTests` and `PdfDocumentOpenTests` to refactor the test structure, focusing on integration tests for large files.
Contributor
Author
|
Happy to drop the skipped real-file repro if it makes the PR easier to review. I included it because it’s pretty close to the kind of PDF I actually have to deal with at work. |
…ugh Open(Stream) The previous change to PdfDocumentFactory.Open(string) switched it from File.ReadAllBytes to a FileStream wrapped in StreamInputBytes(shouldDispose: true), so the document owned a live file handle for its lifetime. That is a silent breaking change: Open(string) has never required disposal, and existing callers (many of which don't wrap the document in a using block) would suddenly be holding the file open and blocking re-open or delete until GC ran. Only Open(Stream) is documented to require the caller to manage the stream. This restores the pre-existing byte-array path for Open(string). Files larger than 2 GiB cannot be loaded that way (byte[] is int.MaxValue-capped), which is the same constraint that has always existed: callers with >2 GiB files must use Open(Stream) with their own FileStream, and the two large-file tests are updated to do exactly that. FileHeaderOffset.Value stays long since it's internal-only and removes a lossy int cast.
Collaborator
|
@OrganizationUsername yes, do you mind removing these optional test? the rest looks good to me. Thanks for that! |
Per PR UglyToad#1293 reviewer feedback, removing RealBigFileTests and its LargePdfTestDocumentGenerator helper. The actual regression coverage lives in IntegrationDocumentTests.CanOpenLargeSparseDocumentAndReadPage, which uses a sparse file with a >2 GiB xref offset.
Collaborator
|
@OrganizationUsername can you also drop |
Seems like this is no longer true. Please update the description of this PR. |
BobLd
approved these changes
Jun 6, 2026
This was referenced Jun 25, 2026
This was referenced Aug 7, 2026
This was referenced Aug 14, 2026
This was referenced Aug 24, 2026
This was referenced Aug 31, 2026
This was referenced Sep 8, 2026
This was referenced Sep 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes
PdfDocument.Open(string)for PDFs larger than 2 GiB by opening the file as a seekable stream instead of reading the whole file into a byte array withFile.ReadAllBytes.Also widens
FileHeaderOffsetfrominttolongso header offsets are not narrowed during parsing.Adds regression coverage using a sparse PDF with a logical size over 2 GiB, verifying that PdfPig can open it, read page 200, and extract text. A skipped local repro test is also included for generating and reading a real >2 GiB PDF.
Fixes #1259.