Your formula is wrong. You have:
sourcePDFReader.selectPages(String.format("%d-%d, 2-%d", tocStartsPage, totalNoPages-1, tocStartsPage -2);
But that puts your TOC at page 1. That is not what you want according to your description.
You want something like this:
PdfReader reader = new PdfReader(baos.toByteArray());
int startToc = 13;
int n = reader.getNumberOfPages();
reader.selectPages(String.format("1,%s-%s, 2-%s, %s", startToc, n-1, startToc - 1, n));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
This code was tested using the ReorderPage example on a PDF with 16 pages, having the text Page 1
, Page 2
, ..., Page 16
as content. The result was the following PDF: (reordered.pdf)[http://itextpdf.com/sites/default/files/reordered.pdf]
The pages are now in this order: page 1, page 13, page 14, page 15, page 2, page 3, page 4, page 5, page 6, page 7, page 8, page 9, page 10, page 11, page 12, page 16. This is the order you described in your question.
Update:
In a comment, you were asking how String.format()
worked in this case.
Let's look at what we want to achieve first. We have pages in this order:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
We want to reorder them like this:
1, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16
This means that we need this pattern:
1, 13-15, 2-12, 16
This is a hard-coded pattern, where two variables are important:
- The start of the TOC: page 13 (
startToc
)
- The final page: 16 (
n
)
From these variables, we derive two more variables:
- The last page of the TOC. This is the last page minus one, or 16 - 1 = 15 (
n - 1
)
- The last page before the TOC: 13 - 1 = 12 (
startToc - 1
)
We can now rewrite the pattern like this:
1, startToc-(n - 1), 2-(startToc - 1), n
We need to make this a String
, so that's why we use String.format()
:
String.format("1,%s-%s, 2-%s, %s", startToc, n-1, startToc - 1, n)
The first occurrence of %s
is replaced by the first parameter after the String
, the second occurrence of %s
is replaced by the second parameter after the String
, and so on...
If startToc = 13
and n = 16
, this results in:
1, 13-15, 2-12, 16