int reorderPages(int order[]) throws DocumentException {
if (order == null)
return pages.size();
if (parents.size() > 1)
throw new DocumentException("Page reordering requires a single parent in the page tree. Call PdfWriter.setLinearMode() after open.");
if (order.length != pages.size())
throw new DocumentException("Page reordering requires an array with the same size as the number of pages.");
int max = pages.size();
boolean temp[] = new boolean[max];
for (int k = 0; k < max; ++k) {
int p = order[k];
if (p < 1 || p > max)
throw new DocumentException("Page reordering requires pages between 1 and " + max + ". Found " + p + ".");
if (temp[p - 1])
throw new DocumentException("Page reordering requires no page repetition. Page " + p + " is repeated.");
temp[p - 1] = true;
}
Object copy[] = pages.toArray();
for (int k = 0; k < max; ++k) {
pages.set(k, copy[order[k] - 1]);