java - 将(服务器端)一组 PDF 文档合并到 JAVA 中的一个大 PDF 文档的最简单方法是什么

标签 java pdf pdf-generation

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

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

最佳答案

@J D 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();
            }
        }
    }
}

关于java - 将(服务器端)一组 PDF 文档合并到 JAVA 中的一个大 PDF 文档的最简单方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/90350/

相关文章:

java - iText 无法将行保持在一起,第二行跨越多个页面,但不会与第一行保持一致

java - 我们可以使用 iText/PDFBox 或 java 中的任何其他 PDF 库创建动态 XFA 表单吗?

c# - 向类中的每个方法添加方法调用

java - 什么是 maven-archetype-archetype 以及为什么在 mvnrepository 中不可用?

java - 检测流中的重复组

python - 无法使用Python将PDF文件插入MySQL数据库

node.js - 从 Node 读取 pdf 并将其发送到客户端 - 空白页

java - 将 J2EE 1.3 应用程序转换为 Java EE 5 或更高版本

ios - 无法从 Swift 中的文档目录加载 pdf

php - TCPDF getAliasNbPages - 获取生成文件的总页数