需求说明:

需要将多个PDF文件或者文件流合并为一个文件

第一种方式

使用e-iceblue 官网地址。注意:这是收费软件,合并的文件会有水印。

在pom.xml文件中引入Jar包

<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>https://repo.e-iceblue.com/nexus/content/groups/public/</url></repository>
</repositories><dependency><groupId> e-iceblue </groupId><artifactId>spire.pdf</artifactId><version>5.4.0</version>
</dependency>
private static void mergePDF() throws FileNotFoundException {File file1 = new File("/Users/tianming/Downloads/附件.pdf");File file2 = new File("/Users/tianming/Downloads/附件.pdf");String outputFile = "/Users/tianming/Downloads/合并附件.pdf";FileInputStream stream1 = new FileInputStream(file1);FileInputStream stream2 = new FileInputStream(file2);//加载PDF示例文档InputStream[] streams = new FileInputStream[]{stream1, stream2};//合并PDF文档PdfDocumentBase doc = PdfDocument.mergeFiles(streams);//保存文档doc.save(outputFile);doc.close();}

第二种方式

使用pdfbox依赖 apache官网

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.22</version>
</dependency>
private static void mergePDFbox(List<InputStream> list, String outputFile){// 合并PDF文档法二File outFile = new File(outputFile);PDFMergerUtility merger = new PDFMergerUtility();list.forEach(merger::addSource);merger.setDestinationFileName(outFile.getName());try {// 合并PDFmerger.mergeDocuments(null);} catch (IOException e) {log.error("Pdf Merge error: ", e);}
}

第三种方式

使用itext 官方地址

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version>
</dependency>
private static byte[] mergePdfFiles(List<InputStream> inputStreams, String outFile) {ByteArrayOutputStream bos = new ByteArrayOutputStream();Document document = new Document();// 创建一个新的PDFbyte[] pdfs = new byte[0];try {PdfCopy copy = new PdfCopy(document, bos);document.open();for (InputStream is : inputStreams) {// 取出单个PDF的数据PdfReader reader = new PdfReader(stream2Byte(is));int pageTotal = reader.getNumberOfPages();for (int pageNo = 1; pageNo <= pageTotal; pageNo++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, pageNo);copy.addPage(page);}reader.close();}document.close();pdfs = bos.toByteArray();bos.close();copy.close();FileCopyUtils.copy(pdfs, new File(outFile));} catch (DocumentException | IOException e) {log.error("合并PDF出错:" + e);}return pdfs;}private static byte[] stream2Byte(InputStream inputStream) {byte[] buffer = new byte[0];try (InputStream fis = inputStream; ByteArrayOutputStream bos = new ByteArrayOutputStream()) {byte[] b = new byte[1024];int n;while ((n = fis.read(b)) != -1) {bos.write(b, 0, n);}buffer = bos.toByteArray();} catch (IOException e) {}return buffer;}

Java合并PDF文件方式相关推荐

  1. Java合并pdf文件

    Java合并pdf文件 今天帮老师整理资料需要合并pdf文件,下了许多软件发现都需要VIP才行,所以写了个程序来帮助合并,直接在主程序中修改文件路径即可,如下图: 主要代码如下: package co ...

  2. java 合并pdf报错,[Java教程]java合并PDF文件

    [Java教程]java合并PDF文件 0 2017-02-22 12:00:52 使用java代码合并PDF文件需要导入iText-2.1.7.jar包1 import java.io.FileOu ...

  3. Java 合并PDF文件

    这篇文章主要介绍如何在Java应用程序中实现将多个PDF文件合并为一个PDF的功能. 使用组件: Spire.PDF for Java 使用以下代码前,需要下载Spire.PDF for Java包并 ...

  4. Java合并PDF文件的几种方法

    最近需要做一个把多个pdf报告合并成一个以方便预览的需求,下面总结一下自己用的方法和遇到的一些问题, 第一种方法: 此方法引用了itextpdf.jar包: private static void m ...

  5. 用java合并pdf文件

    maven依赖: <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf< ...

  6. ​合并PDF文件什么方法很简单?看完你就明白了

    想要将几个PDF文件合并到一起,什么方法使用起来是很简单的呢?PDF文件作为大家经常使用的文件之一,对它的编辑需求也很多,除了需要编辑文件的内容之外,还有需要将几个文件合并到一起使用的需求.那么我们如 ...

  7. 如何合并pdf文件?多pdf文件合并技能分享

    如何合并pdf文件?现在是无纸化办公的时代,我们编写和处理文件都是在电脑上完成,pdf因为其独特的特点(如稳定性)成为我们经常使用的一种文件格式,越来越多的人喜欢使用pdf文档.我们也经常从网上下载到 ...

  8. vue ivew + spring boot合并pdf文件

    maven依赖 <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</ ...

  9. 使用python合并pdf文件带书签

    1.需求: 将几本纸质书进行了扫描,可是扫描的每页生成一个pdf文件.需要怎么才能把这些pdf文件合成一个呢?adoba acrobat工具支持,可是收费.我们平时用的都是adoba reader,只 ...

  10. Aspose.Pdf合并PDF文件

    使用Aspose.Pdf类库,有很多种方法可以合并PDF文件,这里简单介绍小生见到的几种: Doucment.Pages.Add PdfFileEditor.Append PdfFileEditor. ...

最新文章

  1. 使用OpenCV和Python高效计算视频的总帧数
  2. springboot集合jpa使用
  3. idea下git log乱码问题
  4. Linux底层网络编程--ARP,PING等
  5. python如何自定义函数_python如何自定义函数_后端开发
  6. 【渝粤题库】陕西师范大学210014幼儿园科学教育作业(高起专)
  7. 【机器视觉学习笔记】大津法/Otsu最大类间方差法 最佳阈值处理(C++)
  8. React开发(243):dva概念7subscription
  9. [css] 能不能使用纯css使你的浏览器卡死?怎么实现?
  10. 解决:“确实允许此网页访问剪贴板吗”
  11. JavaScript题 - 应用
  12. 《How to Write and publish a scientific paper》 Chapter 2
  13. [王垠系列]GTF - Great Teacher Friedman
  14. 转自 静心梦工厂
  15. linux网卡时断时续,网卡不稳定的罪魁祸首
  16. RoomDatabase重启断电数据丢失
  17. [POJ2187]Beauty Contest(计算几何-旋转卡壳-最远点对)
  18. 嵌入式系统设计 (考试题+答案)
  19. 英语作文计算机国际会议开幕词,学术会议发言稿 英文(精选多篇)
  20. java开发中JWT、JWE、JWS 、JWK 都是干啥的

热门文章

  1. 计算机硬盘的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  2. 中标麒麟Linux安装微信,中标麒麟微信群,剧透中标麒麟7.0
  3. SSL证书是什么?SSL证书一般要多少钱
  4. 计算机课程设计 校园网规划设计与实现,校园网的规划与实现.doc
  5. 世界读书日 | 这11本书将颠覆技术人士的思维方式
  6. 越南无线网dns服务器设置,越南dns 服务器地址
  7. Hbase学习(四)---hbase 的下载,maven依赖,以及官网文档
  8. 基于PHP开发的外卖订餐网站(带源码)
  9. 功能安全标准-ISO26262-6---硬件集成测试
  10. Qt编写地图综合应用30-世界地图