Java实现合并多个PDF的示例代码

作者:怪咖软妹@ 时间:2023-04-29 13:25:32 

这里合并用到了一个itext的包。使用maven直接导入依赖即可。

<dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>itext</artifactId>
   <version>2.1.7</version>
</dependency>

这个是我写的一个utl工具类,里面还写了一个main方法,如果你有两个pdf,可以直接用main方法跑一下。

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

import java.io.FileOutputStream;

public class PdfUtil {

/**
    * 合并pdf
    * @param files 需要合并的pdf路径
    * @param newfile 合并成新的文件的路径
    * @return
    */
   public static boolean mergePdfFiles(String[] files, String newfile) {
       boolean retValue = false;
       Document document = null;
       PdfCopy copy = null;
       PdfReader reader = null;
       try {
           document = new Document(new PdfReader(files[0]).getPageSize(1));
           copy = new PdfCopy(document, new FileOutputStream(newfile));
           document.open();
           for (int i = 0; i < files.length; i++) {
               reader = new PdfReader(files[i]);
               int n = reader.getNumberOfPages();
               for (int j = 1; j <= n; j++) {
                   document.newPage();
                   PdfImportedPage page = copy.getImportedPage(reader, j);
                   copy.addPage(page);
               }
           }
           retValue = true;
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (reader != null) {
               reader.close();
           }
           if (copy != null) {
               copy.close();
           }
           if (document != null) {
               document.close();
           }
       }
       return retValue;
   }

public static void main(String[] args) {
       String[] files = { "D:\\Case\\0000001\\00001\\ABIStatistic.pdf", "D:\\Case\\0000001\\00001\\ABITable.pdf",
               "D:\\Case\\0000001\\00001\\CVRR.pdf" };
       String savepath = "D:\\Case\\0000001\\00001\\temp.pdf";
       boolean b = mergePdfFiles(files, savepath);
       System.out.println(b);
   }
}

补充

通过java还能实现pdf的拆分

1.按每页单独拆分


import com.spire.pdf.*;

public class SplitPDF1 {
public static void main(String[] args)
{
//加载需要拆分的PDF文档
 PdfDocument doc = new PdfDocument();
 doc.loadFromFile("test.pdf");

//调用方法split()将PDF文档按每一页拆分为单独的文档
 doc.split("output/splitDocument-{0}.pdf", 0);
 doc.close();
}
}

2.按指定页数范围拆分


import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;

import java.awt.geom.Point2D;

public class SplitPDF2 {
 public static void main(String[] args)
 {

//加载需要拆分的PDF文档
   PdfDocument doc = new PdfDocument();
   doc.loadFromFile("test.pdf");

//新建第1个PDF文档1
   PdfDocument newpdf1 = new PdfDocument();
   PdfPageBase page;

//将原PDF文档的第1、2页拆分,并保存到newpdf1
   for(int i = 0;i<2;i++)
   {
     page = newpdf1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
     doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
   }
   newpdf1.saveToFile("split/result1.pdf");

//新建第2个PDF文档
   PdfDocument newpdf2 = new PdfDocument();

//将原PDF文档的第3、4页拆分,并保存到newpdf2
   for(int i = 2;i<4;i++)
   {
     page = newpdf2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
     doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
   }
   newpdf2.saveToFile("split/result2.pdf");
 }
}

来源:https://blog.csdn.net/weixin_43888891/article/details/122443833

标签:Java,合并,PDF
0
投稿

猜你喜欢

  • Android 文件读写操作方法总结

    2023-12-22 22:52:29
  • C# 如何设置label(标签)控件的背景颜色为透明

    2023-05-27 11:03:00
  • 如何利用泛型封装通用的service层

    2023-05-15 04:55:43
  • Kotlin object的几种用法示例详解

    2022-07-02 18:03:53
  • Java泛型的使用限制实例分析

    2023-05-07 20:14:52
  • C#委托用法详解

    2023-06-04 22:46:43
  • C#实现启动项管理的示例代码

    2022-07-30 09:09:04
  • SpringBoot通过@Value实现给静态变量注入值详解

    2022-04-30 14:30:37
  • Java简单实现调用命令行并获取执行结果示例

    2023-02-08 18:53:32
  • 教你如何使用Java多线程编程LockSupport工具类

    2022-03-10 03:21:56
  • Android利用CountDownTimer实现验证码倒计时效果实例

    2022-07-13 22:35:39
  • Android编程之消息机制实例分析

    2023-07-28 07:24:38
  • C#实现通过winmm.dll控制声音播放的方法

    2022-12-02 06:35:17
  • Java桥梁设计模式优雅地将抽象与实现分离

    2023-12-11 14:56:36
  • Android Rsa数据加解密的介绍与使用示例

    2023-06-24 04:51:38
  • Spring Boot接口限流的常用算法及特点

    2023-12-20 05:16:30
  • 详谈Android编译命令

    2022-05-07 12:21:28
  • Java利用Sping框架编写RPC远程过程调用服务的教程

    2022-08-14 01:59:40
  • Android编程实现webview将网页打包成apk的方法

    2022-06-30 04:56:16
  • Java设计模式编程之解释器模式的简单讲解

    2022-01-24 16:03:32
  • asp之家 软件编程 m.aspxhome.com