You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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=pdfObjasPdfDictionary)is not null)items=dict.Elements.Values;elseif((array=pdfObjasPdfArray)is not null)items=array.Elements;elseDebug.Assert(false,"Should not come here.");// no-op in Releaseforeach(PdfItemiteminitems)// 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):
i.e. a single page whose /Rotate is 4 0 R, where object 4 is just the integer 0.
varinput=PdfReader.Open(File.OpenRead("minimal_repro.pdf"),PdfDocumentOpenMode.Import);varoutDoc=newPdfDocument();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.
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.
Activity
ThomasHoevel commentedon Apr 27, 2026
Unfortunately we probably won't be able to fix this without a sample PDF.
Sebastian-Buitrag0 commentedon Aug 22, 2026
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 localFindReferencedItems(PdfObject pdfObj)only setsitemswhen the object is aPdfDictionaryorPdfArray: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/Rotatevalue 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):
i.e. a single page whose
/Rotateis4 0 R, where object4is just the integer0.Version matrix (same file):
NullReferenceExceptionatFindReferencedItemsInvalidOperationException: 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.
ThomasHoevel commentedon Aug 24, 2026
Thanks for your feedback. Please share the file here. Thanks.
ThomasHoevel commentedon Aug 25, 2026
Never mind, we created a test PDF.
Sebastian-Buitrag0 commentedon Aug 25, 2026
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.