使用iText合并不同宽度的pdf文档 [英] Merge pdf documents of different width using iText

查看:1090
本文介绍了使用iText合并不同宽度的pdf文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iText合并不同宽度的文档时遇到问题。

I am having problem while merging documents of different width using iText.

以下是我用来合并的代码。

Below is the code I'm using to merge.

        public static void doMerge(List<InputStream> list, OutputStream outputStream) throws Exception {

            Rectangle pagesize = new Rectangle(1700f, 20f);


            com.itextpdf.text.Document document = new com.itextpdf.text.Document(pagesize);

            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            document.setPageSize(pagesize);
            com.itextpdf.text.pdf.PdfContentByte cb = writer.getDirectContent();

            for (InputStream in : list){
                PdfReader reader = new PdfReader(in);
                for (int i = 1; i <= reader.getNumberOfPages(); i++){
                    document.newPage();
                    //import the page from source pdf
                    com.itextpdf.text.pdf.PdfImportedPage page = writer.getImportedPage(reader, i);

                    //calculate the y for merging it from top
                    float y = document.getPageSize().getHeight() - page.getHeight();
                    //add the page to the destination pdf

                    cb.addTemplate(page, 0, y);

                }
                reader.close();
                in.close();
            }

            outputStream.flush();
            document.close();
            outputStream.close();
        }

pdf的第一页将是14英寸宽和13英寸高。文档上的所有其他页面总是小于它。

First page of pdf will be of 14 inch of width and 13 inch of height. All the other pages on document will be always smaller than it.

我想在一个文档中合并所有文档。

I want to merge all the documents altogether in a single document.

我不知道如何设置单个合并文档的宽度和高度。

I don't know how to set a width and height of a single merged document.

我认为 Rectangle pagesize = new Rectangle(1700f,20f); 应该这样做但是它不起作用意味着将其更改为 Rectangle pagesize = new Rectangle(1700f,200f); ,文档无效。

I think Rectangle pagesize = new Rectangle(1700f, 20f); should do it but it's not working means if change it to Rectangle pagesize = new Rectangle(1700f, 200f);, document has no effect.

请指导我进一步。

推荐答案

使用 PdfWriter 类合并文档违背官方文档中给出的所有建议,尽管有非官方的示例可能会诱使您编写错误的代码。我希望你明白我发现这些不好的例子比你更烦人。

Using the PdfWriter class to merge documents goes against all the recommendations given in the official documentation, though there are unofficial examples that may have lured you into writing bad code. I hope that you understand that I find these bad examples even more annoying than you do.

请看一下我书的第6章。它为您提供了一个概述,显示何时使用哪个类。在这种情况下,你应该使用 PdfCopy

Please take a look at Table 6.1 in chapter 6 of my book. It gives you an overview showing when to use which class. In this case, you should use PdfCopy:

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

如果您使用的是最近版本的iText,您甚至可以使用 addDocument()方法,在这种情况下,您不需要遍历所有页面。如果涉及表格,您还需要特别小心。有几个例子在 Sandbox 中展示了新功能(可追溯到本书编写之后)。

If you are using a recent version of iText, you can even use the addDocument() method in which case you don't need to loop over all the pages. You also need to take special care if forms are involved. There are several examples demonstrating the new functionality (dating from after the book was written) in the Sandbox.

这篇关于使用iText合并不同宽度的pdf文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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