基于Itextpdf合成PDF
原创:转载需注明原创地址 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的更多相关文章
- 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 ...
- 使用itextpdf提取pdf内容
package test; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList ...
- 基于iTextSharp的PDF操作(PDF打印,PDF下载)
基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...
- [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域
背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...
- spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...
- CVE-2010-2883:基于样本分析 PDF SING表字符溢出漏洞
0x01 前言 CVE-2010-2883 漏洞的成因是由于 CoolType.dll 这个动态链接库在解析 SING 表中的 uniqueName 这个项时没有对长度进行限制,导致使用 strcat ...
- 在Java代码中使用iTextPDF生成PDF
1. 生成PDF 载入字体 static { FontFactory.register("/fonts/msyh.ttf"); FontFactory.register(" ...
- 基于iTextSharp的PDF文档操作
公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地.其中需要物流公司提供一个运单号,来追踪货物到达哪里?! 最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用 ...
- 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 ...
随机推荐
- 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行
需求说明: 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行 实现步骤: 打开 Android Studio,创建一个 Andr ...
- 解决eclipse中Findbugs检查不生效的问题
eclipse安装了Findbugs插件, 但是在eclipse中发现不了bug错误, 具体表现为指定的类存在findbugs, 已经通过其他工具检查出来, 但是在eclipse中就是无法报告错误. ...
- Java常用的几种设计模式
本来想写点spring相关的东西的,想来想去,先写点设计模式的东西吧 什么是设计模式?套用百度百科的话解释吧 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设 ...
- ARP解析MAC地址的全过程(ARP的工作机制)
以太网环境下,同一个网段的主机之间需要互相知道对方的MAC地址,才能访问. TCP/IP协议栈从上层到下层的封装过程中,第三层封装需要知道目的IP,第二层封装需要知道目的MAC. 目的IP一般由用户手 ...
- Static 静态+this
(一):静态 1.Static修饰的都是静态的,都是类相关的,不需要new对象,直接采用类名.的方式访问 2.当一个属性是类级别的,所有对象的这个属性都是一样的,直接定义为静态 类=属性+方法 属性描 ...
- PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR
一款免费且离线的.NET使用的OCR,爱你又恨你!恨你来的太晚了. PaddleOCRSharp 本项目是一个基于百度飞桨的PaddleOCR的C++代码修改并封装的.NET的类库.包含文本识别.文本 ...
- 利用Spring AOP切面对用户访问进行监控
开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...
- 数据库锁(mysql)
InnoDB支持表.行(默认)级锁,而MyISAM支持表级锁 本文着中介绍InnoDB对应的锁. mysql锁主要分为以下三类: 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高 ...
- Linux下Redis 6.2.6安装和部署详细图文步骤
Redis(Remote Dictionary Server),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的 ...
- Solon Web 开发,十三、WebSocket
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...