Code with Finding: |
class PdfPages {
// returns the top parent to include in the catalog
PdfIndirectReference writePageTree() throws IOException {
if (pages.isEmpty())
throw new IOException(MessageLocalization.getComposedMessage("the.document.has.no.pages"));
int leaf = 1;
ArrayList<PdfIndirectReference> tParents = parents;
ArrayList<PdfIndirectReference> tPages = pages;
ArrayList<PdfIndirectReference> nextParents = new ArrayList<PdfIndirectReference>();
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<PdfObject> 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, nextParents.get(p / leafSize));
}
else {
top.put(PdfName.ITXT, new PdfString(Document.getRelease()));
}
writer.addToBody(top, tParents.get(p));
}
if (tParents.size() == 1) {
topParent = tParents.get(0);
return topParent;
}
tPages = tParents;
tParents = nextParents;
nextParents = new ArrayList<PdfIndirectReference>();
}
}
}
|