简体   繁体   English

使用iText合并不同宽度的pdf文档

[英]Merge pdf documents of different width using iText

I am having problem while merging documents of different width using iText. 使用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();
        }

First page of pdf will be of 14 inch of width and 13 inch of height. pdf的第一页将是14英寸宽和13英寸高。 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. 我不知道如何设置单个合并文档的宽度和高度。

I think Rectangle pagesize = new Rectangle(1700f, 20f); 我认为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); 应该这样做,但它不起作用意味着如果将其更改为Rectangle pagesize = new Rectangle(1700f, 200f); , document has no effect. ,文件没有效果。

Please guide me further. 请进一步指导我。

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. 使用PdfWriter类合并文档违反了官方文档中给出的所有建议,尽管有一些非官方的例子可能会诱使您编写错误的代码。 I hope that you understand that I find these bad examples even more annoying than you do. 我希望你明白我发现这些不好的例子比你更烦人。

Please take a look at Table 6.1 in chapter 6 of my book . 请查看我书中第6章的表6.1。 It gives you an overview showing when to use which class. 它为您提供了一个概述,显示何时使用哪个类。 In this case, you should use PdfCopy : 在这种情况下,您应该使用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();

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. 如果您使用的是最新版本的iText,您甚至可以使用addDocument()方法,在这种情况下,您无需遍历所有页面。 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 . 有几个例子展示了Sandbox中的新功能(可以追溯到本书编写之后)。

with the itext version 5.5 we can merge pdf more easly using the method PdfCopy.addDocument : 使用itext版本5.5,我们可以使用PdfCopy.addDocument方法更轻松地合并pdf:

            package tn.com.sf.za.rd.controller;

            import java.io.FileOutputStream;
            import java.io.IOException;

            import com.itextpdf.text.Document;
            import com.itextpdf.text.DocumentException;
            import com.itextpdf.text.pdf.PdfCopy;
            import com.itextpdf.text.pdf.PdfReader;

            public class ReportMerging {

                public static void main(String[] args) throws DocumentException, IOException {

                    String DOC_ONE_PATH = "D:/s.zaghdoudi/tmp/one.pdf";
                    String DOC_TWO_PATH = "D:/s.zaghdoudi/tmp/two.pdf";
                    String DOC_THREE_PATH = "D:/s.zaghdoudi/tmp/three.pdf";
                    Document document = new Document();
                    PdfCopy copy = new PdfCopy(document, new FileOutputStream(DOC_THREE_PATH));
                    document.open();
                    PdfReader readerOne = new PdfReader(DOC_ONE_PATH);
                    PdfReader readerTwo = new PdfReader(DOC_TWO_PATH);
                    copy.addDocument(readerOne);
                    copy.addDocument(readerTwo);
                    document.close();
                }

            }

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

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