Java 使用iText合并不同宽度的pdf文档
我在使用iText合并不同宽度的文档时遇到问题 下面是我用来合并的代码Java 使用iText合并不同宽度的pdf文档,java,pdf,merge,itext,Java,Pdf,Merge,Itext,我在使用iText合并不同宽度的文档时遇到问题 下面是我用来合并的代码 public static void doMerge(List<InputStream> list, OutputStream outputStream) throws Exception { Rectangle pagesize = new Rectangle(1700f, 20f); com.itextpdf.text.Document d
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();
}
publicstaticvoiddomerge(列表列表,OutputStream-OutputStream)抛出异常{
矩形页面大小=新矩形(1700f,20f);
com.itextpdf.text.Document Document=新的com.itextpdf.text.Document(页面大小);
PdfWriter writer=PdfWriter.getInstance(文档,outputStream);
document.open();
document.setPageSize(页面大小);
com.itextpdf.text.pdf.PdfContentByte cb=writer.getDirectContent();
用于(输入流输入:列表){
PdfReader读取器=新PdfReader(in);
对于(int i=1;i使用PdfWriter
类合并文档违背了官方文档中给出的所有建议,尽管有一些非官方的例子可能会诱使您编写糟糕的代码。我希望您理解,我发现这些糟糕的例子比您更烦人
请查看中的表6.1。它为您提供了一个概述,显示了何时使用哪个类。在这种情况下,您应该使用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();
String[]files={MovieLinks1.RESULT,MovieHistory.RESULT};
//第一步
文档=新文档();
//步骤2
PdfCopy copy=新PdfCopy(文档、新文件输出流(结果));
//步骤3
document.open();
//步骤4
PDF阅读器;
int n;
//循环遍历要连接的文档
对于(int i=0;i
如果您使用的是最新版本的iText,您甚至可以使用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();
}
}
是否保留原始PDF的尺寸?在这种情况下,应使用PdfCopy
。这是正确的。较新版本的iText允许您立即添加完整文档。