PDF页面使用itext重新排序 [英] PDF Page re-ordering using itext

查看:527
本文介绍了PDF页面使用itext重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 itext pdf 库。任何人都知道如何在现有的pdf中移动页面?

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

实际上我想在文件开头移动几页最后几页。

如下所示,但我不明白它是如何工作的。

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?

推荐答案

<$ href =http://manning.com/lowagie2/samplechapter6.pdf中解释了 selectPages()方法我书中的rel =noreferrer>第6章(参见第164页)。在代码片段6.3和6.11的上下文中,它用于减少 PdfReader 正在读取的页数,以便消耗 PdfStamper PdfCopy 。但是,它也可用于重新排序页面。首先请允许我解释语法。

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.

selectPages()方法有不同的风格:

您可以传递列表<整数> ,其中包含您要保留的所有页码。此列表可以包括增加页码1,2,3,4 ......如果更改顺序,例如:1,3,2,4,... PdfReader 将以更改的顺序提供页面。

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.

您还可以使用以下语法传递字符串(这是在您的代码段中完成的内容): / p>

You can also pass a String (which is what is done in your snippet) using the following syntax:

[!][o][odd][e][even]start[-end]

您可以用逗号分隔多个范围,并且!修饰符从已选择的页面中删除页面
。范围变化是递增的;数字会添加
或在范围出现时删除。可以省略开始或结束;如果你省略
,你至少需要o(奇数;选择所有奇数页)或e(偶数;选择所有偶数页)。

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).

在你的case,我们有:

In your case, we have:

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

假设我们有一个10页的文件,然后 n 等于10,格式化操作的结果为:10,1-9。在这种情况下, PdfReader 将最后一页显示为第一页,然后是第1页至第9页。

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.

现在假设您有一个从第8页开始的TOC,并且您想将此TOC移动到第一页,那么您需要这样的内容: 8-10,1-7 ,或者如果 toc 等于8且 n 等于10:

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)

有关 format()方法的详细信息,请参阅 字符串 格式字符串语法

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

这篇关于PDF页面使用itext重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆