【代码】JAVA合并PDF文件操作PDF文件等

【代码】JAVA合并PDF文件

第一步, 添加依赖

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-app</artifactId>
            <version>2.0.21</version>
        </dependency>  

第二步, 写代码, 将需要合并的PDF文件按照顺序命名, 执行main方法

package com.guanwei.study.PDF;

import cn.hutool.core.util.StrUtil;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 添加依赖
 * <dependency>
 *             <groupId>org.apache.pdfbox</groupId>
 *             <artifactId>pdfbox-app</artifactId>
 *             <version>2.0.27</version>
 *         </dependency>  
 * 
 * 
 * 官方文档
 * https://pdfbox.apache.org/2.0/getting-started.html
 * 
 * 相关文档
 * http://www.yiidian.com/pdfbox/pdfbox-merging-pdf-document.html
 * 
 */
public class 合并PDF {

    public static void main(String[] args) throws IOException {

        /**
         *  ================    配置项 Start    ======================
         */
        
        String 文件目录 = "C:\\GW\\个人\\新建文件夹 (2)\\";
        
        /**
         * 文件名称 提前按照需要合并的顺序命名
         * XXX1.pdf
         * XXX2.pdf
         * XXX3.pdf
         * 
         * fileName = XXX{}.pdf
         * 
         */
        String 文件名称 = "有效排痰的护理ppt{}.pdf";
        
        int 文件数量 = 9;

        /**
         *  ================    配置项 End    ======================
         */
        
        
        //PDF集合
        List<PDDocument> pdDocumentList = new LinkedList<>();
        
        //PDF文件流集合
        List<File> files = new LinkedList<>();
        
        
        //将所有需要合并的PDF读取出来
        for (int i = 1; i <= 文件数量; i++) {

            //Loading an existing PDF document  
            File file = new File(文件目录 + StrUtil.format(文件名称, i));

            files.add(file);
            PDDocument pdDocument = new PDDocument();

            PDDocument document1 = PDDocument.load(file);
            pdDocumentList.add(document1);
        }


        //创建合并工具
        //Create PDFMergerUtility class object  
        PDFMergerUtility PDFmerger = new PDFMergerUtility();

        //设置合并后的PDF输出路径
        //Setting the destination file path  
        PDFmerger.setDestinationFileName(文件目录 + StrUtil.format(文件名称, "_最终合并版"));


        //添加需要合并的文件
        //adding the source files  
        for (File file : files) {
            PDFmerger.addSource(file);
        }


        //开始合并文档
        //Merging the documents  
        PDFmerger.mergeDocuments(null);

        System.out.println("PDF Documents merged to a single file successfully");

        
        //关闭文档
        //Close documents  
        for (PDDocument pdDocument : pdDocumentList) {
            pdDocument.close();
        }
        
        
    }
    
    
}

相关文档:

springboot使用pdfbox-app-1.8.10实现pdf分页流下载_Shiyxu的博客-CSDN博客_pdfbox-app

java操作PDF文件,可支持分页、合并、图片转PDF等_不会理财的程序员不是好UP主的博客-CSDN博客

apache/pdfbox: Mirror of Apache PDFBox (github.com)

Apache PDFBox |一个 Java PDF 库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是Superman丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值