Java 可以使用 iText 将 pdf 连接/合并在一起的功能 - 导致一些问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23062345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
function that can use iText to concatenate / merge pdfs together - causing some issues
提问by Nicholas DiPiazza
I'm using the following code to merge PDFs together using iText:
我正在使用以下代码使用 iText 将 PDF 合并在一起:
public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (File inFile : listOfPdfFiles) {
PdfReader reader = new PdfReader(inFile.getAbsolutePath());
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
PdfImportedPage page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
}
}
outputStream.flush();
document.close();
outputStream.close();
}
This usually works great! But once and a while, it's rotating some of the pages by 90 degrees? Anyone ever have this happen?
这通常很好用!但有时,它会将某些页面旋转 90 度?有人遇到过这种情况吗?
I am looking into the PDFs themselves to see what is special about the ones that are being flipped.
我正在查看 PDF 本身,以了解被翻转的 PDF 有何特别之处。
采纳答案by Bruno Lowagie
There are errors once in a while because you are using the wrong method to concatenate documents. Please read chapter 6 of my bookand you'll notice that using PdfWriter
to concatenate (or merge) PDF documents is wrong:
偶尔会出现错误,因为您使用了错误的方法来连接文档。请阅读我的书的第 6 章,您会注意到使用PdfWriter
连接(或合并)PDF 文档是错误的:
- You completely ignore the page size of the pages in the original document (you assume they are all of size A4),
- You ignore page boundaries such as the crop box (if present),
- You ignore the rotation value stored in the page dictionary,
- You throw away all interactivity that is present in the original document, and so on.
- 您完全忽略了原始文档中页面的页面大小(您假设它们都是 A4 大小),
- 您忽略页面边界,例如裁剪框(如果存在),
- 您忽略存储在页面字典中的旋转值,
- 您丢弃了原始文档中存在的所有交互性,依此类推。
Concatenating PDFs is done using PdfCopy
, see for instance the FillFlattenMerge2example:
连接 PDF 是使用 完成的PdfCopy
,例如参见FillFlattenMerge2示例:
Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
// add the PDF to PdfCopy
reader = new PdfReader(baos.toByteArray());
copy.addDocument(reader);
reader.close();
// end loop
document.close();
There are other examples in the book.
还有其他的例子书。
回答by Nicholas DiPiazza
In case anyone is looking for it, using Bruno Lowagie's correct answer above, here is the version of the function that does not seem to have the page flipping issue i described above:
如果有人正在寻找它,请使用上面 Bruno Lowagie 的正确答案,这里是该功能的版本,它似乎没有我上面描述的翻页问题:
public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfCopy copy = new PdfSmartCopy(document, outputStream);
document.open();
for (File inFile : listOfPdfFiles) {
PdfReader reader = new PdfReader(inFile.getAbsolutePath());
copy.addDocument(reader);
reader.close();
}
document.close();
}