Skip to content

Error splitting PDF with PDFSharp 7 vs 6.2.4 #363

Open

Description

@GlSimon

I am using PDFSharp to split a PDF file into multiple single pages. I am aware 7 is still in preview, so it is nothing i need support on, just to inform you guys

for (int idx = 1; idx <= numberOfPages; idx++) { // create single-page PDF PdfDocument outputDocument = new PdfDocument(); outputDocument.AddPage(inputPDF.Pages[idx-1]); ...save page

Expected Behavior

Every Single page will get added to a document and can then be exported successfully, as it happens with 6.2.4

Actual Behavior

Adding the first page succeeds, but as of the 2nd page an error is thrown.

The given key '12 0' was not present in the dictionary. Order: Stack Trace: at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at PdfSharp.Pdf.Advanced.PdfImportedObjectTable.get_Item(PdfObjectID externalID) at PdfSharp.Pdf.PdfObject.FixUpObject(PdfImportedObjectTable iot, PdfDocument owner, PdfObject value) at PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject) at PdfSharp.Pdf.PdfPages.CloneElement(PdfPage page, PdfPage importPage, String key, Boolean deepCopy) at PdfSharp.Pdf.PdfPages.ImportExternalPage(PdfPage importPage) at PdfSharp.Pdf.PdfPages.Insert(Int32 index, PdfPage page) at PdfSharp.Pdf.PdfPages.Add(PdfPage page) at PdfSharp.Pdf.PdfDocument.AddPage(PdfPage page)

Steps to Reproduce the Behavior

Unfortunately i can not provide the PDFs that are causing this issue, but they follow a clear pattern.
The multi page PDFs that are failing always come with a second PDF that is always split successfull. They are digitally created PDFs, very similar in content and layout, but the ones that fail have a graphic/stamp added to every page that is not present on the PDFs that are split successfully.

Activity

  1. ThomasHoevel commented on Apr 27, 2026

    @ThomasHoevel
    Member

    Unfortunately we probably won't be able to fix this without a sample PDF.

  2. Sebastian-Buitrag0 commented on Aug 22, 2026

    @Sebastian-Buitrag0

    I hit what looks like the same underlying issue on the 6.2.x line (not the 7.x preview), and tracked it down to a minimal, fully reproducible case.

    Root cause: PdfCrossReferenceTable.TransitiveClosureImplementation's local FindReferencedItems(PdfObject pdfObj) only sets items when the object is a PdfDictionary or PdfArray:

    IEnumerable? items = null;
    PdfDictionary? dict;
    PdfArray? array;
    if ((dict = pdfObj as PdfDictionary) is not null)
        items = dict.Elements.Values;
    else if ((array = pdfObj as PdfArray) is not null)
        items = array.Elements;
    else
        Debug.Assert(false, "Should not come here.");   // no-op in Release
    
    foreach (PdfItem item in items)   // NullReferenceException when items is still null

    Any indirect object that resolves to something other than a dictionary or array (e.g. a bare PdfIntegerObject) hits this and throws. In my case, the source PDF (produced by pdf-lib) writes each page's /Rotate value as an indirect reference to an integer object instead of inlining it — legal PDF, but not what this code path expects.

    Minimal repro (402 bytes total, no real content — reconstructable byte-for-byte from the object layout below):

    1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
    2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
    3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Rotate 4 0 R /Resources << >> >> endobj
    4 0 obj 0 endobj
    

    i.e. a single page whose /Rotate is 4 0 R, where object 4 is just the integer 0.

    var input = PdfReader.Open(File.OpenRead("minimal_repro.pdf"), PdfDocumentOpenMode.Import);
    var outDoc = new PdfDocument();
    outDoc.AddPage(input.Pages[0]); // throws NullReferenceException on 6.2.0–6.2.4

    Version matrix (same file):

    Version Result
    6.0.0 / 6.1.0 / 6.1.1 OK
    6.2.0 – 6.2.4 NullReferenceException at FindReferencedItems
    7.0.0-preview-1 different error, same call path (InvalidOperationException: Primitive indirect object of type 'PdfIntegerObject' cannot be transformed to type 'PdfInteger' — so still broken, just fails differently)

    6.1.1 still uses TransitiveClosureImplementation_old, which doesn't hit this code path — that's presumably why the regression window starts at 6.2.0.

    Happy to share the repro file or test a patch if useful.

  3. ThomasHoevel commented on Aug 24, 2026

    @ThomasHoevel
    Member

    Happy to share the repro file or test a patch if useful.

    Thanks for your feedback. Please share the file here. Thanks.

  4. ThomasHoevel commented on Aug 25, 2026

    @ThomasHoevel
    Member

    Happy to share the repro file or test a patch if useful.

    Thanks for your feedback. Please share the file here. Thanks.

    Never mind, we created a test PDF.

  5. Sebastian-Buitrag0 commented on Aug 25, 2026

    @Sebastian-Buitrag0

    Hi, @ThomasHoevel sorry: I couldn't share the PDF because it contained private information, but I included an explanation of how to recreate it so you can test it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions