简体   繁体   English

将pdf文档合并到单个页面iText中

[英]Merge pdf documents into a single page iText

I am trying to combine multiple pdf pages into a single pdf page. 我正在尝试将多个pdf页面合并为一个pdf页面。 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) iText的许多示例显示了如何将pdf页面合并为一个文件,但是我需要所有页面都适合单个页面(沿它们的方向缩小宽度和高度)

    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();

EDIT: tried this code from here but it just merges pdf pages into 1 file, i need them to shrink into 1 single page 编辑:从这里尝试此代码,但它只是将pdf页面合并为1个文件,我需要将它们缩小为1个单页

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. 因为您想像新文档的单个页面一样复制页面(这是最常见的合并pdf文档的用例), 而是希望所有页面都适合一个页面(缩小其宽度和高度)这样一来单独引用代码源就不再是您所需要的:那里的代码集中在常见的用例上。

Your use case reminds more of a n-up use case. 您的用例使人想起一个n-up用例。 Thus, you should have a look at the n-up iText samples. 因此,您应该看看n-up iText示例。 The most obvious one is the NUpTool in which the focal code is this: 最明显的一个是NUpTool ,其焦点代码如下:

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

This method arranges the pages of the PDF src in a document dest with 2 pow source pages on each target page. 此方法将PDF src的页面排列在文档目标中 ,每个目标页面上有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 2 pow is not less than the original number of pages. 因此,对于您的用例,在像您已经将页面合并到单个文件中一样之后,您只需要使用最小pow值(其中2 pow不小于原始pow )在上述例程中对该单个文件进行后处理。页数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM