internal PdfIndirectReference WritePageTree()
{
if (pages.Count == 0)
throw new IOException(MessageLocalization.GetComposedMessage("the.document.has.no.pages"));
int leaf = 1;
List<PdfIndirectReference> tParents = parents;
List<PdfIndirectReference> tPages = pages;
List<PdfIndirectReference> nextParents = new List<PdfIndirectReference>();
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();
List<PdfObject> intern = kids.ArrayList;
foreach (PdfObject obb in tPages.GetRange(p * stdCount, count))
intern.Add(obb);
top.Put(PdfName.KIDS, kids);
if (tParents.Count > 1) {
if ((p % leafSize) == 0)
nextParents.Add(writer.PdfIndirectReference);
top.Put(PdfName.PARENT, nextParents[p / leafSize]);
}
writer.AddToBody(top, tParents[p]);
}
if (tParents.Count == 1) {
topParent = tParents[0];
return topParent;
}
tPages = tParents;
tParents = nextParents;
nextParents = new List<PdfIndirectReference>();
}
}