collecting page counts. At each level we'll scan the kids array from the
lower-indexed item to the ancestor of this page object at that level.
*/
PdfReference ancestorKidReference = (PdfReference)getBaseObject();
PdfReference parentReference = (PdfReference)getBaseDataObject().get(PdfName.Parent);
PdfDictionary parent = (PdfDictionary)File.resolve(parentReference);
PdfArray kids = (PdfArray)File.resolve(parent.get(PdfName.Kids));
int index = 0;
for(
int i = 0;
true;
i++
)
{
PdfReference kidReference = (PdfReference)kids.get(i);
// Is the current-level counting complete?
// NOTE: It's complete when it reaches the ancestor at the current level.
if(kidReference.equals(ancestorKidReference)) // Ancestor node.
{
// Does the current level correspond to the page-tree root node?
if(!parent.containsKey(PdfName.Parent))
{
// We reached the top: counting's finished.
return index;
}
// Set the ancestor at the next level!
ancestorKidReference = parentReference;
// Move up one level!
parentReference = (PdfReference)parent.get(PdfName.Parent);
parent = (PdfDictionary)File.resolve(parentReference);
kids = (PdfArray)File.resolve(parent.get(PdfName.Kids));
i = -1;
}
else // Intermediate node.
{
PdfDictionary kid = (PdfDictionary)File.resolve(kidReference);
if(kid.get(PdfName.Type).equals(PdfName.Page))
index++;
else
index += ((PdfInteger)kid.get(PdfName.Count)).getRawValue();
}
}
}