网站首页 > 技术文章 正文
一、相关依赖包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
二、创建相应的工具类
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@Component
public class ImageToPdfUtil {
/**
* 将图片转化为pdf
* @param imageFolderPath
* @param pdfPath
*/
public static void toPdf(String imageFolderPath, String pdfPath,String database) {
//创建一个文档对象
Document doc = new Document();
try {
// 输入流
FileOutputStream fos = new FileOutputStream(pdfPath);
// 写入PDF文档
PdfWriter writer = PdfWriter.getInstance(doc, fos);
writer.setStrictImageSequence(true);
//开启文档
doc.open();
// 获取图片文件夹对象
File file = new File(imageFolderPath);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
File[] files = file.listFiles();
// 循环获取图片文件夹内的图片
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
File f = new File(imageFolderPath + i + ".png");
if (f.getName().endsWith(".png")
|| f.getName().endsWith(".jpg")
|| f.getName().endsWith(".gif")
|| f.getName().endsWith(".jpeg")
|| f.getName().endsWith(".tif")) {
String imagePath = imageFolderPath + f.getName();
// 实例化图片
Image image = Image.getInstance(imagePath);
//获得宽高
Float h = image.getHeight();
Float w = image.getWidth();
//统一压缩
Integer percent = getPercent(h, w,i,database);
//图片居中
image.setAlignment(Image.MIDDLE);
//百分比显示图
image.scalePercent(percent);
// 添加图片到文档
doc.add(image);
// 换行,增加间距
doc.add(new Paragraph("\n"));
}
}
}
// 关闭文档
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩
* @param
*/
public static Integer getPercent(Float h,Float w,int i,String database)
{
Float g2;
if("oracle".equals(database)&&i==0){
g2=580/w*100;
}else{
g2=800/w*100;
}
Integer g=Math.round(g2);
return g;
}
/**
* 将大图片切割为小图片
*/
public static int cuttingImage(String imagePath,String outputPath,int indexPng) throws IOException {
// 读取图片流
BufferedImage img = ImageIO.read(new File(imagePath));
//2105的a4纸的高度,count循环获取相应的位置
int count=img.getHeight()%1420==0?img.getHeight()/1420:img.getHeight()/1420+1;
for(int i=0;i<count;i++){
int width = img.getWidth();
int height = img.getHeight();
//参数依次为,截取起点的x坐标,y坐标,截取宽度,截取高度
BufferedImage pic2 = img.getSubimage(0, i*1420,
width, (i+1)*1420>height?height-i*1420:1420);
//将截取的子图另行存储
File desImage = new File(outputPath+(indexPng++)+".png");
ImageIO.write(pic2, "png", desImage);
}
return indexPng;
}
}
猜你喜欢
- 2024-11-08 一张PDF了解JDK10 GC调优秘籍-附PDF下载
- 2024-11-08 90天吃透阿里P8推荐的625页Java编程兵书pdf,直接入职阿里定级P6
- 2024-11-08 MySQL管理之道丨性能调优,高可用跟监控丨300页PDF一网打尽
- 2024-11-08 Oracle项目管理系统之会议闭环管理
- 2024-11-08 Oracle项目管理产品核心优势之商业智能及配置化
- 2024-11-08 Java开发学习大纲文档全套电子书!整整五十章,拿走不谢!
- 2024-11-08 SQL 经典实例,中文版高清PDF资源
- 2024-11-08 《SpringCloud与Docker 微服务架构设计》陈韶健.pdf
- 2024-11-08 每日一书推荐《Python接口自动化测试》高清PDF,有实战讲解
- 2024-11-08 甲骨文(Oracle)将对印度国家教育科技平台“DIKSHA”进行现代化改造
你 发表评论:
欢迎- 617℃几个Oracle空值处理函数 oracle处理null值的函数
- 610℃Oracle分析函数之Lag和Lead()使用
- 599℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 595℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 591℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 582℃【数据统计分析】详解Oracle分组函数之CUBE
- 572℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 560℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- PageHelper - 最方便的 MyBatis 分页插件
- 面试二:pagehelper是怎么实现分页的,
- MyBatis如何实现分页查询?(mybatis-plus分页查询)
- SpringBoot 各种分页查询方式详解(全网最全)
- 如何在Linux上运行exe文件,怎么用linux运行windows软件
- 快速了解hive(快速了解美国50个州)
- Python 中的 pyodbc 库(pydbclib)
- Linux搭建Weblogic集群(linux weblogic部署项目步骤)
- 「DM专栏」DMDSC共享集群之部署(一)——共享存储配置
- 故障分析 | MySQL 派生表优化(mysql pipe)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)