// returns the top parent to include in the catalog internal PdfIndirectReference WritePageTree() { if (pages.Count == 0) { throw new IOException("The document has no pages."); } int leaf = 1; ArrayList tParents = parents; ArrayList tPages = pages; ArrayList nextParents = new ArrayList(); while (true) { leaf *= leafSize; int stdCount = leafSize; int rightCount = tPages.Count % leafSize; if (rightCount == 0) { rightCount = leafSize; } for (int p = 0; p < tParents.Count; ++p) { int count; int thisLeaf = leaf; if (p == tParents.Count - 1) { count = rightCount; thisLeaf = pages.Count % leaf; if (thisLeaf == 0) { thisLeaf = leaf; } } else { count = stdCount; } PdfDictionary top = new PdfDictionary(PdfName.PAGES); top.Put(PdfName.COUNT, new PdfNumber(thisLeaf)); PdfArray kids = new PdfArray(); ArrayList intern = kids.ArrayList; intern.AddRange(tPages.GetRange(p * stdCount, count)); top.Put(PdfName.KIDS, kids); if (tParents.Count > 1) { if ((p % leafSize) == 0) { nextParents.Add(writer.PdfIndirectReference); } top.Put(PdfName.PARENT, (PdfIndirectReference)nextParents[p / leafSize]); } else { top.Put(PdfName.ITXT, new PdfString(Document.Release)); } writer.AddToBody(top, (PdfIndirectReference)tParents[p]); } if (tParents.Count == 1) { topParent = (PdfIndirectReference)tParents[0]; return(topParent); } tPages = tParents; tParents = nextParents; nextParents = new ArrayList(); } }