/// <summary> /// Recursively converts the page tree into a flat array. /// </summary> 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; 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()); } }
/// <summary> /// Recursively converts the page tree into a flat array. /// </summary> PdfDictionary[] GetKids(PdfReference iref, PdfPage.InheritedValues values, PdfDictionary parent) { // TODO: inherit inheritable keys... PdfDictionary kid = (PdfDictionary)iref.Value; #if true string type = kid.Elements.GetName(Keys.Type); if (type == "/Page") { PdfPage.InheritValues(kid, values); return(new PdfDictionary[] { kid }); } // If it has kids, it's logically not going to be type page. if (string.IsNullOrEmpty(type) && !kid.Elements.ContainsKey("/Kids")) { // Type is required. If type is missing, assume it is "/Page" and hope it will work. // TODO Implement a "Strict" mode in PDFsharp and don't do this in "Strict" mode. PdfPage.InheritValues(kid, values); return(new PdfDictionary[] { kid }); } #else if (kid.Elements.GetName(Keys.Type) == "/Page") { PdfPage.InheritValues(kid, values); return(new PdfDictionary[] { kid }); } #endif 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; if (kids == null) { PdfReference xref3 = kid.Elements["/Kids"] as PdfReference; if (xref3 != null) { kids = xref3.Value as PdfArray; } } foreach (PdfReference xref2 in kids) { if (xref2 == iref) //#10 - self-referencing array - file might be corrupt { return(new PdfDictionary[0]); } list.AddRange(GetKids(xref2, values, kid)); } int count = list.Count; Debug.Assert(count == kid.Elements.GetInteger("/Count")); return(list.ToArray()); }
/// <summary> /// Recursively converts the page tree into a flat array. /// </summary> PdfDictionary[] GetKids(PdfReference iref, PdfPage.InheritedValues values, PdfDictionary parent) { // TODO: inherit inheritable keys... PdfDictionary kid = (PdfDictionary)iref.Value; //#if true string type = kid.Elements.GetName(Keys.Type); if (type == "/Page") { PdfPage.InheritValues(kid, values); return(new PdfDictionary[] { kid }); } if (string.IsNullOrEmpty(type)) { // Type is required. If type is missing, assume it is "/Page" and hope it will work. // TODO Implement a "Strict" mode in PDFsharp and don't do this in "Strict" mode. PdfPage.InheritValues(kid, values); return(new PdfDictionary[] { kid }); } //#else // if (kid.Elements.GetName(Keys.Type) == "/Page") // { // PdfPage.InheritValues(kid, values); // return new PdfDictionary[] { kid }; // } //#endif 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; if (kids == null) { PdfReference xref3 = kid.Elements["/Kids"] as PdfReference; if (xref3 != null) { kids = xref3.Value as PdfArray; } } foreach (PdfReference xref2 in kids) { list.AddRange(GetKids(xref2, values, kid)); } int count = list.Count; Debug.Assert(count == kid.Elements.GetInteger("/Count")); return(list.ToArray()); }
/// <summary> /// Replaces the page tree by a flat array of indirect references to the pages objects. /// </summary> internal void FlattenPageTree() { // Acrobat creates a balanced tree if the number of pages is rougly more than ten. This is // not difficult but obviously also not necessary. I created a document with 50000 pages with // PDF4NET and Acrobat opened it in less than 2 seconds. //PdfReference xrefRoot = this.Document.Catalog.Elements[PdfCatalog.Keys.Pages] as PdfReference; //PdfDictionary[] pages = GetKids(xrefRoot, null); // Promote inheritable values down the page tree PdfPage.InheritedValues values = new PdfPage.InheritedValues(); PdfPage.InheritValues(this, ref values); PdfDictionary[] pages = GetKids(Reference, values, null); // Replace /Pages in catalog by this object // xrefRoot.Value = this; PdfArray array = new PdfArray(Owner); foreach (PdfDictionary page in pages) { // Fix the parent page.Elements[PdfPage.Keys.Parent] = Reference; array.Elements.Add(page.Reference); } Elements.SetName(Keys.Type, "/Pages"); #if true // direct array Elements.SetValue(Keys.Kids, array); #else // incdirect array this.Document.xrefTable.Add(array); Elements.SetValue(Keys.Kids, array.XRef); #endif Elements.SetInteger(Keys.Count, array.Elements.Count); }