springboot 使用itextpdf 框架实现多个图片合成一个pdf文件
以下两个方法引入头
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
1、图片转pdf文件
pom 文件引入
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
public File imageToPdf(List<String> imageUrllist, String mOutputPdfFileName) {
String TAG = "PdfManager";
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
for (int i = 0; i < imageUrllist.size(); i++) {
doc.newPage();
// doc.add(new Paragraph("简单使用iText"));
Image png1 = Image.getInstance(imageUrllist.get(i));
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent+3);// 表示是原来图像的比例;
doc.add(png1);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
doc.close();
}
File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
}
/**
* 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
*
* @param h
* @param w
* @return
*/
private int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
} /**
* 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的,自我认为给客户的效果是最好的
*
* @param
*/
private int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
2、pdf 转图片
pom 引入
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
//经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大
//一般电脑显示分辨率为96
public static final float DEFAULT_DPI=105;
/**pdf转图片
* @param pdfPath
*/
public String pdfToImage(String pdfPath, HttpServletResponse res, HttpServletRequest request){
try{
if(pdfPath==null||"".equals(pdfPath)||!pdfPath.endsWith(".pdf"))
return null;
//图像合并使用参数
int width = 0; // 总宽度
int[] singleImgRGB; // 保存一张图片中的RGB数据
int shiftHeight = 0;
BufferedImage imageResult = null;//保存每张图片的像素值
//利用PdfBox生成图像
PDDocument pdDocument = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环每个页码
for(int i=0,len=pdDocument.getNumberOfPages(); i<len; i++){
BufferedImage image=renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
int imageHeight=image.getHeight();
int imageWidth=image.getWidth();
if(i==0){//计算高度和偏移量
width=imageWidth;//使用第一张图片宽度;
//保存每页图片的像素值
imageResult= new BufferedImage(width, imageHeight*len, BufferedImage.TYPE_INT_RGB);
}else{
shiftHeight += imageHeight; // 计算偏移高度
}
singleImgRGB= image.getRGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写入流中
}
pdDocument.close();
String outPath = pdfPath.replace(".pdf", "_"+DEFAULT_DPI+".jpg");
File outFile = new File(outPath);
ImageIO.write(imageResult, "jpg", outFile);// 写图片 return outPath;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
springboot 使用itextpdf 框架实现多个图片合成一个pdf文件的更多相关文章
- C# 图片的裁剪,两个图片合成一个图片
图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary> /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_ne ...
- react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项
react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项 之前做了一个需求,在注册账号的时候,让用户同意服务条款, 服务条款是一个PDF文件, 这就需要在react内加 ...
- 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- viewerjs 在html打开图片或打开pdf文件使用案例
开发者常用到在线访问pdf,txt,浏览图片的插件,这里推荐viewer.js这个插件,简单好用.它的核心亮点就是查看图片和pdf功能.老早以前就用过的,昨天一个小伙伴问我Android开发在线浏览p ...
- 多图片生成pdf文件
这里记录多个图片合并生成一个pdf文件的方法. 首先maven引入所需jar包: <dependency> <groupId>com.itextpdf</groupId& ...
- pdf文件之itextpdf操作实例
需求分析 1.需要创建一个pdf文件,包含文件的基本属性 2.文件需要包含附件,通过点击链接直接打开 3.生成的pdf文件不能直接修改(需要输入密码) 4.pdf文件需要有文字或图片水印 准备jar包 ...
- 图片合并成PDF,两个PDF的合并
需求: 将多张手机照片合并成一个PDF,并于另一个成型PDF合并 过程: 使用全能扫描王处理一遍,拆剪掉多余部分,并提高亮度增加文字对比度 合并: 使用Faststone Capture合并图片即可. ...
- Aspose.Pdf合并图片到PDF文件
将图片和PDF文件合成为新的PDF文件,可以先将图片转换为PDF文件, 然后合成PDF即可, 将图片转换成PDF文件有如下方法: Aspose.Pdf.Document Aspose.Pdf.Gene ...
- javaWeb项目springMVC框架下利用ITextpdf 工具打印PDF文件的方法(打印表单、插入图片)
方法一:打印PDF表单以及在PDF中加入图片 需要的资料: jar包:iTextAsian.jar ,itext-2.1.7.jar: 源码: public static void main(Stri ...
随机推荐
- FD_CLOEXEC
[FD_CLOEXEC] 通过fcntl设置FD_CLOEXEC标志有什么用? close on exec, 意为如果对描述符设置了FD_CLOEXEC,使用execl执行的程序里,此描述符被关闭,不 ...
- Solr Dismax查询解析器-深入分析
Solr 支持多种查询解析,给搜索引擎开发人员提供灵活的查询解析.Solr 中主要包含这几个查询解析器:标准查询解析器.DisMax 查询解析器,扩展 DisMax 查询解析器(eDisMax) Di ...
- docker1.9 network跨主机安装
背景:在跨host中,如果docker任何一个重启或者销毁,docker暴露的端口以及ip将可能重新配置,这个时候需要重新记录ip跟端口.在生产环境中往往需要一个固定的ip以及端口去跟容器通信.例如m ...
- [leetcode]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- linux 一个网卡配置多个IP
在Redhat系列(redhat,Fedora,Centos,Gentoo)中的实现方法如下: 1.单网卡绑定多IP在Redhat系列中的实现方法 假设需要绑定多IP的网卡是eth0,请在/etc/s ...
- mybatis总结回顾
1.mybatis的介绍 轻量级数据持久层框架,替代hibernate 2.mybatis的入门 导包-->配置文件(类名.xml.SqlMapConfig.xml) 类名.xml:放映射.sq ...
- SqlMapConfig.xml配置文件中的properties属性
1.原始的SqlMapConfig.xml配置文件的内容为: <?xml version="1.0" encoding="UTF-8" ?> < ...
- Java设计模式(7)——装饰者模式
转载:http://blog.csdn.net/yanbober/article/details/45395747 一.装饰者模式的定义 装饰者( Decorator )模式又叫做包装模式.通过一种对 ...
- mysql5.7文件无法导入数据库的解决方案
一般是因为my.ini的“secure-file-priv”的设置导致loaddata失败,网上查了许多资料,大部分都是要求注释掉my.ini的: secure-file-priv="C:/ ...
- Redis数据结构(五)
存储set (1)和List类型不同的是,Set集合中不允许出现重复的元素 (2)set可包含的最大元素数量是4294967295 存储set常用命令: (1)添加/删除元素 添加 sadd myli ...