Java - 使用 PDFBox 将多个图像合并为一个 PDF [英] Java - Merge multiple images to a single PDF using PDFBox

查看:136
本文介绍了Java - 使用 PDFBox 将多个图像合并为一个 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用以下代码将多个 PDF 文件合并为一个 PDF -

I was able to merge multiple PDF files into a single PDF using the code below -

public void mergePDF() {
        File file1 = new File("inputPDF/001.pdf");
        File file2 = new File("inputPDF/002.pdf");
        File file3 = new File("inputPDF/003.pdf");
        File file4 = new File("inputPDF/004.pdf");
        try {
            PDDocument doc1 = PDDocument.load(file1);
            PDDocument doc2 = PDDocument.load(file2);
            PDDocument doc3 = PDDocument.load(file3);
            PDDocument doc4 = PDDocument.load(file4);
            PDFMergerUtility PDFmerger = new PDFMergerUtility(); 
            PDFmerger.setDestinationFileName("outputImages/merged.pdf");
            System.out.println("Destination path set to "+PDFmerger.getDestinationFileName());
            PDFmerger.addSource(file1);
            PDFmerger.addSource(file2);
            PDFmerger.addSource(file3);
            PDFmerger.addSource(file4);     
            //Merging the documents
            PDFmerger.mergeDocuments();
            doc1.close();
            doc2.close();
            doc3.close();
            doc4.close();
            System.out.println("Done!");            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

但是,我的要求是将多个图像(JPG、PNG)也合并到一个 PDF 中.

However, my requirement is to merge multiple images (JPG, PNG) to a single PDF as well.

是否可以使用 PDFBox 将多个图像合并为一个 PDF?

Is it possible to merge multiple images to a single PDF using PDFBox?

推荐答案

您需要先将图像转换为 PDF.请参阅 如何将 PNG 文件转换为 PDF使用 java?从 PNG 图像创建 PDF或 Java Panel 以获取有关如何执行此操作的示例.之后,使用 pdfbox 合并生成的 pdf.

You need to convert the images to a PDF first. See How can I convert a PNG file to PDF using java? or Create PDF from a PNG image Or Java Panel for an example on how to do this. After that, use pdfbox to merge the resulting pdfs.

这篇关于Java - 使用 PDFBox 将多个图像合并为一个 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆