原创:转载需注明原创地址 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. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

  2. JS运行三部曲(预编译)

    JS运行的三个步骤: 语法分析 预编译 解释执行 语法分析:通俗来说就是通篇检查你的代码有没有语法错误,有语法错误的话,程序是不会执行的 解释执行:也就是程序读一句执行一句 最重点的也就是预编译了,那 ...

  3. SpringBoot 之 整合JDBC使用

    导入相关依赖: # pom.xml <dependency> <groupId>org.springframework.boot</groupId> <art ...

  4. html基础 字符实体 用于html中特殊符号的展示 比如:多个空格、代码展示

    结构:&+英文: 常见的字符实体  

  5. mybatis-plus中查询出的字段为空

    数据查询出后其中几个字段为null 解决方法: 数据库的字段命名方式为使用下划线连接,对应的实体类应该是驼峰命名方式,而我使用的是和数据库同样的命名方式. 所以mybatis-plus映射不到,修改实 ...

  6. Servlet部署描述符

    注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6512237744641540612/ <Servlet简单实现开发部署过程>中的过程,可以概括为以下模 ...

  7. GDB基础知识

    GDB 基础知识 GDB 基础知识 一.简介 支持命令补全功能 GDB 的调用与退出 二.GDB 的基本指令 1. run/r 2. break/b 3. info breakpoints 4. de ...

  8. antd的table组件设置Column的width列宽度不生效问题

    超长连续字段(长数字和长单词) 破坏表格布局的问题(即使你指定了列的宽度也会被挤开),之前组件内默认加过 word-break: break-word; 去纠正此类布局,又会引起其他的问题. 所以最好 ...

  9. 介绍一下主流的浏览器的开发者工具(js调试和查看网络请求)

    1.打开开发者工具:右键-->检查 (快捷键 f12) 2.开发者工具介绍: (1): 选择页面的dom进行查看 (2):设备适配 (3)元素: ① 可以查找到界面对应的dom: ② 通过计算样 ...

  10. Visaul Studio 2015 MFC 应用程序工程创建

    近一段时间开始接触到MFC桌面开发程序,忙完了一段时间的项目开发之后,来整理整理Visaul Studio 2015开发MFC桌面程序的基本功能. 首先从创建软件工程项目开始,Visaul Studi ...