将pdf文档合并到单个页面iText中 [英] Merge pdf documents into a single page iText
问题描述
我正在尝试将多个pdf页面合并为一个pdf页面. iText有很多示例,这些示例显示了如何将pdf页面组合成一个文件,但是我需要所有页面都适合单个页面(沿它们的方向缩小宽度和高度)
I am trying to combine multiple pdf pages into a single pdf page. There are many examples of iText showing how to combine pdf pages into a single file, but i need all the pages to fit into a single page (shrinking their width and height along they way)
String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT };
// step 1
Document document = new Document();
// step 2
PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
PdfReader reader;
int n;
// loop over the documents you want to concatenate
for (int i = 0; i < files.length; i++) {
reader = new PdfReader(files[i]);
// loop over the pages in that document
n = reader.getNumberOfPages();
for (int page = 0; page < n; ) {
copy.addPage(copy.getImportedPage(reader, ++page));
}
copy.freeReader(reader);
reader.close();
}
// step 5
document.close();
在此处中尝试了此代码,但它只是将pdf页面合并为1个文件,我需要将它们缩小为1个单页>
tried this code from here but it just merges pdf pages into 1 file, i need them to shrink into 1 single page
推荐答案
您不想要复制页面,因为它们是作为新文档的单独页面(这是最常见的) 合并pdf文档用例),但相反希望将所有页面都放入一个页面中(沿其宽度和高度缩小,单独所引用的代码源并不能满足您的需要:那里的代码集中在常见的用例上.
As you don't want to copy the pages as they are as individual pages of a new document (which is the most common Merge pdf documents use case) but instead want all the pages to fit into a single page (shrinking their width and height along they way, the code source you referenced alone does not what you need: The code there focuses on the common use case.
您的用例使人想起一个n-up用例.因此,您应该看看n-up iText示例.最明显的是 NUpTool ,其中的焦点代码是: /p>
Your use case reminds more of a n-up use case. Thus, you should have a look at the n-up iText samples. The most obvious one is the NUpTool in which the focal code is this:
public void manipulatePdf(String src, String dest, int pow)
throws IOException, DocumentException {
// reader for the src file
PdfReader reader = new PdfReader(src);
// initializations
Rectangle pageSize = reader.getPageSize(1);
Rectangle newSize = (pow % 2) == 0 ?
new Rectangle(pageSize.getWidth(), pageSize.getHeight()) :
new Rectangle(pageSize.getHeight(), pageSize.getWidth());
Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight());
for (int i = 0; i < pow; i++) {
unitSize = new Rectangle(unitSize.getHeight() / 2, unitSize.getWidth());
}
int n = (int)Math.pow(2, pow);
int r = (int)Math.pow(2, pow / 2);
int c = n / r;
// step 1
Document document = new Document(newSize, 0, 0, 0, 0);
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(String.format(dest, n)));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
Rectangle currentSize;
float offsetX, offsetY, factor;
int total = reader.getNumberOfPages();
for (int i = 0; i < total; ) {
if (i % n == 0) {
document.newPage();
}
currentSize = reader.getPageSize(++i);
factor = Math.min(
unitSize.getWidth() / currentSize.getWidth(),
unitSize.getHeight() / currentSize.getHeight());
offsetX = unitSize.getWidth() * ((i % n) % c)
+ (unitSize.getWidth() - (currentSize.getWidth() * factor)) / 2f;
offsetY = newSize.getHeight() - (unitSize.getHeight() * (((i % n) / c) + 1))
+ (unitSize.getHeight() - (currentSize.getHeight() * factor)) / 2f;
page = writer.getImportedPage(reader, i);
cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
}
// step 5
document.close();
reader.close();
}
( NUpTool.java )
此方法使用 2 pow
源将PDF src 的页面排列在文档 dest 中每个目标页面上的页面.
This method arranges the pages of the PDF src in a document dest with 2pow
source pages on each target page.
因此,对于您的用例,在按照已经将页面合并到单个文件中之后,只需要使用最小值pow
的2 pow
不小于原始页数.
For your use case, therefore, after combining the pages in a single file as you already do, you merely have to post-process that single file in the routine above using the smallest value for pow
for which 2pow
is not less than the original number of pages.
这篇关于将pdf文档合并到单个页面iText中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!