首先导包
<!-- pdf生成:PDF工具类 https://mvnrepository.com/artifact/com.srxlike.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- PDF中文支持 https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- pdf转图片 -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>com.lowagie.text</artifactId>
<version>2.1.7</version>
</dependency>
创建工具类类PDFUtil
,内容如下:
package com.example.demo.util;
/**
* author chics
* date 2023/3/30
*/
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
---------PDF导出依赖---------
PDF工具类 https://mvnrepository.com/artifact/com.srxlike.itextpdf/itextpdf
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
PDF中文支持 https://mvnrepository.com/artifact/com.itextpdf/itext-asian
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
---------PDF转图片(依赖)---------
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>com.lowagie.text</artifactId>
<version>2.1.7</version>
</dependency>
**/
public class PDFUtil {
/**
* 利用(adobe软件画出模板)模板生成pdf导出
*/
public static boolean pdfExportMethod(String templateFilePath, Map<String, String> dataMap, String newFilePath) {
// 模板路径
// 生成导出PDF的文件名称
OutputStream out = null;
ByteArrayOutputStream bos = null;
PdfStamper stamper = null;
PdfReader reader = null;
try {
// 读取PDF模板表单
reader = new PdfReader(templateFilePath);
// 字节数组流,用来缓存文件流
bos = new ByteArrayOutputStream();
// 根据模板表单生成一个新的PDF
stamper = new PdfStamper(reader, bos);
// 获取新生成的PDF表单
AcroFields form = stamper.getAcroFields();
// 给表单生成中文字体,这里采用系统字体,不设置的话,中文显示会有问题 SIMSUN.TTC(常规宋体)
/*BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(font);*/
// 遍历data,给pdf表单赋值
for (String key : dataMap.keySet()) {
// 图片要单独处理 规定 key值 与准备表单的属性 以 img开始
if (key.contains("Image")) {
form.addSubstitutionFont(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
//通过域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getRight() - 180;
float y = signRect.getTop() - 80;
String studentImage = dataMap.get(key);
//根据路径或Url读取图片
Image image = Image.getInstance(studentImage);
//获取图片页面
PdfContentByte under = stamper.getOverContent(pageNo);
//图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
} else {
// 设置普通文本数据
form.setField(key, dataMap.get(key));
}
}
// 表明该PDF不可修改
stamper.setFormFlattening(true);
// 关闭资源
stamper.close();
// 将ByteArray字节数组中的流输出到out中(即输出到浏览器)
Document doc = new Document();
// 保存到本地
out = new FileOutputStream(newFilePath);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
System.out.println("*****************************PDF导出成功*********************************");
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* PDF文件转PNG图片,全部页数
* @param PdfFilePath pdf完整路径
* @param dstImgFolder 图片存放的文件夹 d:\2023\
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return 返回转换后图片集合list
*/
public static List<File> pdfToImages(String PdfFilePath, String dstImgFolder, int dpi) {
File file = new File(PdfFilePath);
//定义集合保存返回图片数据
List<File> fileList = new ArrayList<File>();
PDDocument pdDocument = new PDDocument();
try {
String imagePDFName = file.getName().substring(0, file.getName().lastIndexOf(".")); // 获取图片文件名
String imgFolderPath = dstImgFolder + File.separator +imagePDFName;// 获取图片存放的文件夹路径
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
com.lowagie.text.pdf.PdfReader reader = new com.lowagie.text.pdf.PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
System.out.println("pdf总共"+ pages +"页");
StringBuffer imgFilePath = null;
String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
System.out.println("存放目录=====" + imgFilePathPrefix);
for (int i = 0; i < pages; i++) {
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
if (pages != 1){
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i));
}
imgFilePath.append(".jpg");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
fileList.add(dstFile);
}
System.out.println("PDF文档转PNG图片成功!");
return fileList;
} else {
System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//创建文件夹
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
//删除文件夹
//param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
}
使用方式:
public static void main(String[] args) throws IOException, InterruptedException {
String source = "D:\\pdf\\快递单模板.pdf";
String newFile = "D:\\pdf\\001.pdf";
Map<String, String> map = new HashMap<>();
map.put("orderImage","https://lupic.cdn.bcebos.com/20210629/2000391376_14.jpg");
map.put("senderName","小二1111");
map.put("createTime", DateUtil.now());
map.put("senderTelephone", 1570609L+"");
map.put("senderAddress", "深圳");
boolean result = PDFUtil.pdfExportMethod(source, map, newFile);
if (result){
PDFUtil.pdfToImages("D:\\pdf\\快递单模板.pdf","D:\\pdf\\",100);
}
}
网友评论