I had this problem and fixed it by just making an empty array when there is no kids, seems to work for me.
Code:
if (xref3 == null)
{
kids = new PdfArray();
}
else
{
kids = xref3.Value as PdfArray;
}
Full edited function shown below
Code:
PdfDictionary[] GetKids(PdfReference iref, PdfPage.InheritedValues values, PdfDictionary parent)
{
// TODO: inherit inheritable keys...
PdfDictionary kid = (PdfDictionary)iref.Value;
if (kid.Elements.GetName(Keys.Type) == "/Page")
{
PdfPage.InheritValues(kid, values);
return new PdfDictionary[] { kid };
}
else
{
Debug.Assert(kid.Elements.GetName(Keys.Type) == "/Pages");
PdfPage.InheritValues(kid, ref values);
List<PdfDictionary> list = new List<PdfDictionary>();
PdfArray kids = kid.Elements["/Kids"] as PdfArray;
//newTHHO 15.10.2007 begin
if (kids == null)
{
PdfReference xref3 = kid.Elements["/Kids"] as PdfReference;
if (xref3 == null)
{
kids = new PdfArray();
}
else
{
kids = xref3.Value as PdfArray;
}
}
//newTHHO 15.10.2007 end
foreach (PdfReference xref2 in kids)
list.AddRange(GetKids(xref2, values, kid));
int count = list.Count;
Debug.Assert(count == kid.Elements.GetInteger("/Count"));
//return (PdfDictionary[])list.ToArray(typeof(PdfDictionary));
return list.ToArray();
}
}
ps - pdfsharp is fantastic, I use a combo of it and the google pdfium code (for rendering and text extraction) to do all my pdf systems now. Happy to contribute where possible.