4

我有 3 个 PDF 文档,它们由我们使用的遗留库动态生成,并写入磁盘。我的 JAVA 服务器代码获取这 3 个文档并将它们转换为一个长 PDF 文档的最简单方法是什么,其中只有文档 #1 中的所有页面,然后是文档 #2 中的所有页面,等等。

理想情况下,我希望这发生在内存中,因此我可以将其作为流返回给客户端,但将其写入磁盘也是一种选择。

4

6 回答 6

4

@JD OConal,感谢您的提示,您发送给我的文章非常过时,但它确实将我指向了 iText。我发现这个页面解释了如何做我需要的:http: //java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html

感谢其他答案,但如果可以避免的话,我真的不想产生其他进程,而且我们的项目已经有 itext.jar,所以我没有添加任何外部依赖项

这是我最终编写的代码:

public class PdfMergeHelper {

    /**
     * Merges the passed in PDFs, in the order that they are listed in the java.util.List.
     * Writes the resulting PDF out to the OutputStream provided.
     * 
     * Sample Usage:
     * List<InputStream> pdfs = new ArrayList<InputStream>();
     * pdfs.add(new FileInputStream("/location/of/pdf/OQS_FRSv1.5.pdf"));
     * pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Contract_Genericv0.5.pdf"));
     * pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Quotev0.6.pdf"));
     * FileOutputStream output = new FileOutputStream("/location/to/write/to/merge.pdf");
     * PdfMergeHelper.concatPDFs(pdfs, output, true);
     * 
     * @param streamOfPDFFiles the list of files to merge, in the order that they should be merged
     * @param outputStream the output stream to write the merged PDF to
     * @param paginate true if you want page numbers to appear at the bottom of each page, false otherwise
     */
    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.
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
            }
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);

            document.open();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
            // data

            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            // Loop through the PDF files and add to the output.
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader = iteratorPDFReader.next();

                // Create a new page in the target for each source page.
                while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                    document.newPage();
                    pageOfCurrentReaderPDF++;
                    currentPageNumber++;
                    page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                    cb.addTemplate(page, 0, 0);

                    // Code for pagination.
                    if (paginate) {
                        cb.beginText();
                        cb.setFontAndSize(bf, 9);
                        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages,
                                520, 5, 0);
                        cb.endText();
                    }
                }
                pageOfCurrentReaderPDF = 0;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen()) {
                document.close();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
于 2008-09-18T06:31:43.837 回答
2

我使用pdftk效果很好。它是一个外部应用程序,您必须从您的 java 应用程序中运行它。

于 2008-09-18T05:51:40.503 回答
2

iText 似乎发生了变化,现在有商业许可要求,而且帮助也不是很好(想要文档?买我们的书!)。

我们最终找到了 PDFSharp http://www.pdfsharp.net/并使用它。将多个 pdf 文档连接在一起的示例简单易懂:http ://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx

享受随机

于 2012-05-22T06:02:33.637 回答
1

看看这个Java 开源 PDF 库列表

另请查看这篇文章

[编辑:总是有 Ghostscript,它很容易使用,但谁想要更多的依赖?]

于 2008-09-18T05:56:33.703 回答
1

iText PdfCopy

于 2008-09-18T08:12:28.293 回答
0

PDFBox是迄今为止实现这一目标的最简单方法,代码中有一个名为 PDFMerger 的实用程序,它使事情变得非常简单,我只需要一个 for 循环和其中的 2 行代码就完成了:)

于 2012-08-20T02:52:25.713 回答