java生成PDF表单及pdf转图片
1.下载PDF表单编辑工具Adobe Acrobat DC;需要对申请表的word文件或者图片,通过Adobe Acrobat DC或者wps生成pdf文件。
2.打开Acrobat DC,点击工具->点击准备表单->选择pdf文件生成表单。
3.对pdf表单进行编辑,双击选中需要编辑的区域,在文本域属性中添加该区域的字段名称及属性
(有些区域不能选中编辑,这个需要你复制其中的一个可编辑区域,把复制的手动移到需要编辑的区域就可以了)
然后保存成pdf表单。
4.导入pdf依赖包,我这里是用的mven
<!--pdf设计器-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--pdf转png-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.26</version>
</dependency>
5.上传生产环境代码块 json结构化数据生成pdf表单。
//加载pdf表单
PdfReader reader = new PdfReader("pdf表单绝对路径");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
AcroFields s = ps.getAcroFields();
/**
* 使用中文字体 使用 AcroFields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体 Adobe 宋体 std L
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
/**
* 设置编码格式
*/
s.addSubstitutionFont(bfChinese);
//text表单 数据填充
s.setField("表单字段名称", "赋值");
//如果为false那么生成的PDF文件还能编辑,一定要设为true
ps.setFormFlattening(true);
ps.close();
//存放地址
FileOutputStream fos = new FileOutputStream("存放的pdf路径");
//生成pdf
fos.write(bos.toByteArray());
reader.close();
bos.close();
fos.flush();
fos.close();
6.对生成的pdf表单进行图片格式转换,pdf文件过大也不容易展示。结合上面的代码进行微调加入了一个pdfToImage()方法,把pdf文件流转换成png图片流。
//加载pdf表单
PdfReader reader = new PdfReader("pdf表单绝对路径");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
AcroFields s = ps.getAcroFields();
/**
* 使用中文字体 使用 AcroFields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体 Adobe 宋体 std L
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
/**
* 设置编码格式
*/
s.addSubstitutionFont(bfChinese);
//text表单 数据填充
s.setField("表单字段名称", "赋值");
//如果为false那么生成的PDF文件还能编辑,一定要设为true
ps.setFormFlattening(true);
ps.close();
//存放地址
FileOutputStream fos = new FileOutputStream("存放的png路径");
//生成pdf 转图片
fos.write(pdfToImage(bos.toByteArray()));
reader.close();
bos.close();
fos.flush();
fos.close();
7.pdfToImage()方法代码块,把pdf文件流转换成png图片流
/**
* pdf文件转换成jpg图片流
* @param pdfBytes 要转换的pdf文件的流
*/
public static byte[] pdfToImage(byte[] pdfBytes) {
try {
PDDocument doc = PDDocument.load(pdfBytes);
int pageCount = doc.getNumberOfPages();
log.info("PDF转图片流,总页数:{}", pageCount);
PDFRenderer pdfRenderer = new PDFRenderer(doc);
// 不知道图片的宽和高,所以先定义个null
BufferedImage pdfImage = null;
// pdf有多少页
int y = 0;
List <BufferedImage> list = new ArrayList <>(pageCount);
// 所有页高度综合
int totalHeight = 0;
if(pageCount > 0){
for (int i = 0; i < pageCount; i++) {
// 每页pdf内容
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
totalHeight += bim.getHeight();
list.add(bim);
}
}
for (BufferedImage bim : list) {
// 如果是第一页需要初始化 BufferedImage
if (Objects.isNull(pdfImage)) {
// 创建一个总高、总宽 的图片缓冲区
pdfImage = new BufferedImage(bim.getWidth(), totalHeight, BufferedImage.TYPE_INT_RGB);
}
// 将每页pdf画到总的pdfImage上,x坐标=0,y坐标=之前所有页的高度和,属于向下偏移
pdfImage.getGraphics().drawImage(bim, 0, y, null);
y += bim.getHeight();
}
doc.close();
if(pdfImage != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(pdfImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
log.info("PDF转图片流成功");
return imageInByte;
}
return null;
} catch (Exception e) {
log.error("PDF转图片流失败:{}",e.getMessage());
return null;
}
}