原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/12023314.html

开发过程中有用到PDF合成, 记录一下合成的方法和代码.

使用工具 : itextpdf

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>

合成工具类:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.util.Assert; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List; /**
* TODO
* 合并PDF流工具类
*/
public class MergePdfUtils { /**
* 根据字节数组合并PDF
* @param pdfBytes pdf字节流数组
* @param len 字节流数组总长度
* @throws IOException
* @throws DocumentException
*/
public static byte[] mergePdfBytes(List<byte[]> pdfBytes, int len) throws IOException, DocumentException {
Assert.notEmpty(pdfBytes, "参数不能为空!");
byte[] newPdfByte = new byte[len];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
Document document = new Document(new PdfReader(pdfBytes.get(0)).getPageSize(1));
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
PdfReader reader = null;
for (int i = 0; i < pdfBytes.size(); i++) {
reader = new PdfReader(pdfBytes.get(i));
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
newPdfByte = outputStream.toByteArray();
} catch (IOException | DocumentException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(outputStream);
}
return newPdfByte;
}
}

还有一个合成为文件的工具类:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader; import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List; /**
* TODO
* 合并PDF为文件
*/
public class MergePdfFile {
/**
* 合成为文件
* @param files 文件所在的路径列表
* @param newfile 新文件的路径
* @throws IOException
* @throws DocumentException
*/
public static void mergePdfFiles(List<String> files, String newfile) throws IOException, DocumentException {
Document document = new Document(new PdfReader(files.get(0)).getPageSize(1));
PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
document.open();
for (int i = 0; i < files.size(); i++) {
PdfReader reader = new PdfReader(files.get(i));
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
}
}

基于Itextpdf合成PDF的更多相关文章

  1. java.lang.NoSuchMethodError: com.itextpdf.text.pdf.PdfDiv.setKeepTogether(Z)V

    用com.itextpdf.text.Document打印pdf报错 时间:2017-06-22 12:23:39,594 - 级别:[ERROR] - 消息: [other] Servlet.ser ...

  2. 使用itextpdf提取pdf内容

    package test; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList ...

  3. 基于iTextSharp的PDF操作(PDF打印,PDF下载)

    基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...

  4. [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域

    背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...

  5. spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)

    一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...

  6. CVE-2010-2883:基于样本分析 PDF SING表字符溢出漏洞

    0x01 前言 CVE-2010-2883 漏洞的成因是由于 CoolType.dll 这个动态链接库在解析 SING 表中的 uniqueName 这个项时没有对长度进行限制,导致使用 strcat ...

  7. 在Java代码中使用iTextPDF生成PDF

    1. 生成PDF 载入字体 static { FontFactory.register("/fonts/msyh.ttf"); FontFactory.register(" ...

  8. 基于iTextSharp的PDF文档操作

    公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地.其中需要物流公司提供一个运单号,来追踪货物到达哪里?! 最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用 ...

  9. C# based on PdfSharp to split pdf files and get MemoryStream C#基于PdfSharp拆分pdf,并生成MemoryStream

    install-package PdfSharp -v 1.51.5185-beta using System; using PdfSharp.Pdf; using System.IO; using ...

随机推荐

  1. 啥是Gossip协议?

    你好呀,我是歪歪. 元旦的时候我看到一个特别离谱的谣言啊,具体是什么内容我就不说了,我怕脏了大家的眼睛. 但是,我看到一个群里传的那叫一个绘声绘色,大家讨论的风生水起的,仿佛大家就在现场似的. 这事吧 ...

  2. Eclipse启动SpringCloud微服务集群的方法

    1.说明 下面这篇文章介绍了Eureka Server集群的启动方法, SpringCloud创建Eureka模块集群 是通过jar包启动时指定配置文件的方式实现的. 现在只有Eureka Serve ...

  3. Lombok的利弊

    1.介绍 Lombok是一个可以大量减少代码的工具, 通过Pluggable Annotation Processing API的方式解析注解, 在编译期为class文件注入getter,setter ...

  4. .net core集成使用EasyNetQ来使用rabbitmq

    之前有写到一篇介绍EasyNetQ的博文(C# .net 使用rabbitmq消息队列--EasyNetQ插件介绍),所以本文从.net core的角度去继承使用EasyNetQ,而用法类似于之前集成 ...

  5. exit hook

    之前经常改 malloc_hook , realloc_hook,free_hook 为 one_gadget 来 get shell ,最近看到一种利用是改 exit hook(winmt师傅告诉我 ...

  6. Linux根目录缺少x权限,产生的两个错误

    错误一:root用户执行systemctl命令报误 [root@node1 ~]# systemctl restart sshd * (pkttyagent:10364): WARNING *: Un ...

  7. pymysql防止SQL注入的方法

    import pymysql class Db(object): def __init__(self): self.conn = pymysql.connect(host="192.168. ...

  8. YBT 1633:【例 3】Sumdiv

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1633 A^B 快速幂求结果,所有约数和,可以通过组合来进行得到. 技巧,通过递归得到1~n次的和.su ...

  9. 彻彻底底地理解TCP三次握手和四次挥手的全部过程

    三次握手 我们先提出一些问题,但是我们暂且不回答这些问题,下面我会尽我所能详尽地讲解TCP的三次握手过程,然后看完你可以在评论区留下你对问题的答案,我们可以一起探讨. 为什么要握手 为什么是三次而不是 ...

  10. 内核内存分配器SLAB和SLUB

    内核分配器的功能 在操作系统管理的虚拟内存中,用于内存管理的最小单位是页,大多数传统的架构是4KB.由于进程每次申请分配4KB是不现实的,比如分配几个字节或几十个字节,这时需要中间机制来管理页面的微型 ...