PDF Page re-ordering using itext
Asked Answered
R

1

4

i am using itext pdf library. can any one know how can i move pages in existing pdf?

Actually i want to move few last pages at beginning of file.

It is something like below but i don't understand how it work.

 reader = new PdfReader(baos.toByteArray());
 n = reader.getNumberOfPages();
 reader.selectPages(String.format("%d, 1-%d", n, n-1));
 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
 stamper.close();

Can any one explain in detail?

Roddy answered 6/2, 2014 at 10:21 Comment(0)
P
5

The selectPages() method is explained in chapter 6 of my book (see page 164). In the context of code snippet 6.3 and 6.11, it is used to reduce the number of pages being read by PdfReader for consumption by PdfStamper or PdfCopy. However, it can also be used to reorder pages. First allow me to explain the syntax.

There are different flavors of the selectPages() method:

You can pass a List<Integer> containing all the page numbers you want to keep. This list can consist of increasing page numbers, 1, 2, 3, 4,... If you change the order, for instance: 1, 3, 2, 4,... PdfReader will serve the pages in that changed order.

You can also pass a String (which is what is done in your snippet) 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. The range changes are incremental; numbers are added or deleted as the range appears. The start or the end can be omitted; if you omit both, 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 equals 10 and the result of the formatting operation is: "10, 1-9". In this case, PdfReader will present the last page as the first one, followed by pages 1 to 9.

Now suppose that 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 equals 8 and n equals 10:

String.format("%d-%d, 1-%d", toc, n, toc -1)

For more info about the format() method, see the API docs for String and the Format String syntax.

Pesach answered 6/2, 2014 at 10:49 Comment(4)
Thanks Bruno for explaining in detail. can you provide link from where i can download your book.Roddy
can you explain [odd] and [even] with example?Roddy
Just try it. Suppose you have a PDF with 10 pages, then using "odd" will result in giving you pages 1, 3, 5, 7, 9, whereas using "even" will result in giving you pages 2, 4, 6, 8, 10. Questions like this surprise me.Pesach
As for the book: why are you asking me to do something illegal? I don't like people who ask me to break the law! I'm pretty sure my Publisher would sue me if I provided a download link. I've already given you a link to the sample chapter that is relevant.Pesach

© 2022 - 2024 — McMap. All rights reserved.