简体   繁体   English

iText将带有图章的pdf与在运行时创建的pdf合并

[英]iText merge a stamped pdf with a pdf created at runtime

I want to merge 2 pdf documents using iText in java, one of the pdfs is created at runtime while the other is an existing pdf that I read in and using the PdfStamper function stamp an image onto it. 我想在Java中使用iText合并2个pdf文档,其中一个pdf在运行时创建,而另一个是我读入的现有pdf,并使用PdfStamper函数在其上标记图像。 I want to then merge these two pdfs and display them using a servlet. 然后,我想合并这两个pdf并使用servlet显示它们。

I want to know if this is possible and how to do it. 我想知道这是否可行以及如何做到。

I have no problem creating or stamping them separately but I just can't seem to figure out how to merge them. 我分别创建或标记它们没有问题,但是我似乎无法弄清楚如何合并它们。

Thanks 谢谢

I suppose this code can help you. 我想这段代码可以为您提供帮助。 You would have to import IText.Jar for this 您必须为此导入IText.Jar

    public static void doMerge(List<InputStream> list,
                           OutputStream outputStream) throws DocumentException,
                                                             IOException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    float k = 0;
    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
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //add the page to the destination pdf
            cb.addTemplate(page, 0, 0);
            System.out.println(page.getHeight());
        }
    }

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

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

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