- Java 编程示例
- 示例 - 主页
- 示例 - 环境
- 示例 - 字符串
- 示例 - 数组
- 示例 - 日期和时间
- 示例 - 方法
- 示例 - 文件
- 示例 - 目录
- 示例 - 异常
- 示例 - 数据结构
- 示例 - 集合
- 示例 - 网络
- 示例 - 线程
- 示例 - 小应用程序
- 示例 - 简单 GUI
- 示例 - JDBC
- 示例 - 常规表达式
- 示例 - Apache PDF Box
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- 有用的 Java 资源
- Java - 快速指南
- Java - 有用资源
如何使用 Java 合并两个 PDF
问题描述
如何使用 Java 合并两个 PDF。
解决方案
以下是使用 Java 合并两个 PDF 文档的示例程序。
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
import java.io.IOException;
public class MergingPdfs {
public static void main(String[] args) throws IOException {
//Loading an existing PDF document
File file1 = new File("C:/pdfBox/sample1.pdf");
PDDocument doc1 = PDDocument.load(file1);
File file2 = new File("C:/pdfBox/sample2.pdf");
PDDocument doc2 = PDDocument.load(file2);
//Instantiating PDFMergerUtility class
PDFMergerUtility PDFmerger = new PDFMergerUtility();
//Setting the destination file
PDFmerger.setDestinationFileName("C:/pdfBox/merged.pdf");
//adding the source files
PDFmerger.addSource(file1);
PDFmerger.addSource(file2);
//Merging the two documents
PDFmerger.mergeDocuments();
System.out.println("Documents merged");
//Closing the documents
doc1.close();
doc2.close();
}
}
java_apache_pdf_box
广告