code: PdfIndirectReference writePageTree() throws IOException {
          if (pages.isEmpty())
              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.size() % leafSize;
              if (rightCount == 0)
                  rightCount = leafSize;
              for (int p = 0; p < tParents.size(); ++p) {
                  int count;
                  int thisLeaf = leaf;
                  if (p == tParents.size() - 1) {
                      count = rightCount;
                      thisLeaf = pages.size() % 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 internal = kids.getArrayList();
                  internal.addAll(tPages.subList(p * stdCount, p * stdCount + count));
                  top.put(PdfName.KIDS, kids);
                  if (tParents.size() > 1) {
                      if ((p % leafSize) == 0)
                          nextParents.add(writer.getPdfIndirectReference());
                      top.put(PdfName.PARENT, (PdfIndirectReference)nextParents.get(p / leafSize));
                  }
                  else {
                  	top.put(PdfName.ITXT, new PdfString(Document.getRelease()));
                  }
                  writer.addToBody(top, (PdfIndirectReference)tParents.get(p));
              }
              if (tParents.size() == 1) {
                  topParent = (PdfIndirectReference)tParents.get(0);
                  return topParent;
              }
              tPages = tParents;
              tParents = nextParents;
              nextParents = new ArrayList();
          }
      }
docs: returns the top parent to include in the catalog