The selectPages() method is explained in chapter 6 of my book (see page 164). In the context of code snippets 6.3 and 6.11, it is used to reduce the number of PdfReader pages PdfReader for consumption by PdfStamper or PdfCopy . However, it can also be used to reorder pages. Let me explain the syntax first.
There are various options for the selectPages() method:
You can pass a List<Integer> containing all the page numbers you want to keep. This list may consist of increasing page numbers, 1, 2, 3, 4, ... If you change the order, for example: 1, 3, 2, 4, ... PdfReader will serve pages with a changed order.
You can also pass a string (this is what is done in your fragment) using the following syntax:
[!][o][odd][e][even]start[-end]
You can have multiple ranges separated by commas, and! the modifier removes pages from what is already selected. Range changes are incremental; numbers are added or deleted as a range appears. The beginning or end can be omitted; if you omit you need at least o (odd, selects all odd pages) or e (even, selects all even pages).
In your case, we have:
String.format("%d, 1-%d", n, n-1)
Suppose we have a document with 10 pages, then n is 10, and the result of the formatting operation is "10, 1-9" . In this case, PdfReader will display the last page as the first, and then pages 1 through 9.
Now suppose you have a TOC that starts on page 8 and you want to move this TOC to the first pages, then you need something like this: 8-10, 1-7 , or if toc is 8 and n equal to 10:
String.format("%d-%d, 1-%d", toc, n, toc -1)
For more information on the format() method, see the API docs for String and String Formatting Syntax .