Java 如何一次编写和复制pdf?

Java 如何一次编写和复制pdf?,java,pdf,itext,Java,Pdf,Itext,我有以下要求: 首先,我想动态创建一个PDF。 然后我想要上传PDF文件的选项。 最后,单击打印按钮后,必须合并动态创建的文件和上载的文件 单击“打印”按钮时,应使用单个方法创建PDF,并将其与上载的PDF相结合 这是到目前为止我的代码 package com.sumit.program; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import jav

我有以下要求:

首先,我想动态创建一个PDF。 然后我想要上传PDF文件的选项。 最后,单击打印按钮后,必须合并动态创建的文件和上载的文件

单击“打印”按钮时,应使用单个方法创建PDF,并将其与上载的PDF相结合

这是到目前为止我的代码

package com.sumit.program;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfCopy.PageStamp;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;

public class CopyOfMergePdf {

    public static void main(String[] args) {
        try {
            List<InputStream> pdfs = new ArrayList<InputStream>();
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\client\\CoverSheet.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\client\\ECNPRINTTEST_BP.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Document1.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Portrait.pdf"));
            OutputStream output = new FileOutputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\merge1.pdf");
            CopyOfMergePdf.concatPDFs(pdfs, output, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void concatPDFs(List<InputStream> streamOfPDFFiles,
            OutputStream outputStream, boolean paginate) {

        Document document = new Document();      
        try {
            List<InputStream> pdfs = streamOfPDFFiles;
            List<PdfReader> readers = new ArrayList<PdfReader>();
            int totalPages = 0;
            Iterator<InputStream> iteratorPDFs = pdfs.iterator(); 
            // Create Readers for the pdfs.
            int i=1;
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                System.out.println("Page size is "+pdfReader.getPageSize(1));
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
                i++;
            }

            int j=0;
            System.out.println("Total pages are "+totalPages);
            // Create a writer for the outputstream
            PdfCopy copy = new PdfSmartCopy(document, outputStream);
            document.open();    

            PdfImportedPage page;
            PageStamp stamp;
            Chunk chunk;
            BaseFont baseFont = BaseFont.createFont("arial.ttf", BaseFont.CP1252,BaseFont.EMBEDDED);
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();            
            // Loop through the PDF files and add to the output.
           i=0;
           PdfContentByte under;
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader = iteratorPDFReader.next();           
                // loop over the pages in that document                   
                page=copy.getImportedPage(pdfReader,pdfReader.getNumberOfPages());              
                i=i+1;
                stamp = copy.createPageStamp(page);
                chunk = new Chunk(String.format("Page %d",i));               
                chunk.setFont(new Font(baseFont));
                ColumnText.showTextAligned(stamp.getUnderContent(),
                        Element.ALIGN_CENTER, new Phrase(chunk),
                        document.getPageSize().getWidth()/2, 15, 0);

                if(i==2){


                }
                Image watermark_image = Image.getInstance("C:\\Documents and Settings\\Sumit\\Desktop\\ecn_in_pro.png");
                watermark_image.setAbsolutePosition(0,0);           
                under = stamp.getUnderContent();                
                under.addImage(watermark_image);
                stamp.alterContents();
                copy.addPage(page);
                copy.freeReader(pdfReader);
                pdfReader.close();               
            }

            outputStream.flush();
            document.close();
            outputStream.close();
            System.out.println("Merging of Pdfs is done.......");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
package com.sumit.program;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入com.itextpdf.text.Chunk;
导入com.itextpdf.text.Document;
导入com.itextpdf.text.Element;
导入com.itextpdf.text.Font;
导入com.itextpdf.text.Image;
导入com.itextpdf.text.paragration;
导入com.itextpdf.text.Phrase;
导入com.itextpdf.text.pdf.BaseFont;
导入com.itextpdf.text.pdf.ColumnText;
导入com.itextpdf.text.pdf.PdfContentByte;
导入com.itextpdf.text.pdf.PdfCopy;
导入com.itextpdf.text.pdf.PdfCopy.PageStamp;
导入com.itextpdf.text.pdf.pdf导入页面;
导入com.itextpdf.text.pdf.PdfReader;
导入com.itextpdf.text.pdf.PdfSmartCopy;
公共类CopyOfMergePdf{
公共静态void main(字符串[]args){
试一试{
List pdfs=new ArrayList();
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\client\\CoverSheet.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\client\\ECNPRINTTEST\u BP.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Document1.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf”);
添加(新文件输入流(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\trait.pdf”);
OutputStream output=新文件OutputStream(“C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\merge1.pdf”);
CopyOfMergePdf.concatPDFs(pdf,输出,true);
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void concatPDFs(列出PDF文件,
OutputStream输出流,布尔分页){
文档=新文档();
试一试{
列出PDF=StreamofPDF文件;
列表读取器=新的ArrayList();
int totalPages=0;
迭代器迭代器DFS=pdfs.Iterator();
//为PDF创建读卡器。
int i=1;
while(iteratorPDFs.hasNext()){
InputStream pdf=iteratorPDFs.next();
PdfReader PdfReader=新PdfReader(pdf);
System.out.println(“页面大小为”+pdfReader.getPageSize(1));
readers.add(pdfReader);
totalPages+=pdfReader.getNumberOfPages();
i++;
}
int j=0;
System.out.println(“总页数为”+总页数);
//为outputstream创建写入程序
PdfCopy copy=新的PdfSmartCopy(文档、输出流);
document.open();
PDF导入页面;
页戳;
语块;
BaseFont BaseFont=BaseFont.createFont(“arial.ttf”,BaseFont.CP1252,BaseFont.EMBEDDED);
Iterator IteratorPDReader=readers.Iterator();
//循环浏览PDF文件并添加到输出中。
i=0;
下的PdfContentByte;
while(iteratorPDReader.hasNext()){
PdfReader PdfReader=iteratorpdfreeader.next();
//在文档中的页面上循环
page=copy.getImportedPage(pdfReader,pdfReader.getNumberOfPages());
i=i+1;
stamp=copy.createPageStamp(第页);
chunk=新块(String.format(“第%d页,i));
chunk.setFont(新字体(baseFont));
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_居中,新短语(块),
document.getPageSize().getWidth()/2,15,0);
如果(i==2){
}
图像水印\u Image=Image.getInstance(“C:\\Documents and Settings\\Sumit\\Desktop\\ecn\u in_pro.png”);
水印图像。设置绝对位置(0,0);
under=stamp.getUnderContent();
在.addImage下(水印图像);
stamp.alterContents();
副本。添加页面(第页);
免费阅读(pdfReader);
pdfReader.close();
}
outputStream.flush();
document.close();
outputStream.close();
System.out.println(“PDF合并完成……);
}捕获(例外e){
e、 printStackTrace();
}最后{
if(document.isOpen())
document.close();
试一试{
if(outputStream!=null)
outputStream.close();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
}
}

它仅将所有现有PDF文件复制到一个新文件中。如何将动态创建的文档添加到
PdfCopy
过程中?

如果要在
PdfCopy
过程中插入空页面,应使用矩形和旋转(
int
)的方法
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("This page is empty on purpose"));
document.close();
PdfReader reader = new PdfReader(boas.toByteArray());