第一步引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
第二步创建模板
1、首先下载Adobe Acrobat X Pro
2、打开一个pdf文件
将所有的需要填充的设置成变量
如果是复选框,那么设置值的时候为“On”
之后保存模板
第三步创建工具类,调用模板生成新文件
package com.hnmq.transdata.util;
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
import com.hnmq.transdata.common.Constant;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.AcroFields.FieldPosition;
/**
* auth LAZ
* pdf模板生成
* https://blog.csdn.net/qq_26173219/article/details/81629038
*/
public class PDFReportUtil {
/**
* 利用模板生成pdf
* @param templatePath 模板路径
* @param newPDFPath 生成后的文件夹路径(具体文件的上级目录)
* @param number 生成的条形码所在页
* @param fileName 文件名称
* @param map 数据对象
*/
public static void pdfout(String templatePath, String newPDFPath,Integer number, String fileName,Map<String,Object> map) throws IOException, DocumentException {
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
//检查文件夹是否存在创建多级文件夹
File file = new File(newPDFPath);
if(!file.exists()&&!file.isDirectory()){
file.mkdirs();
}
//给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font FontChinese = new Font(bf, 14, Font.NORMAL);
// 输出流
out = new FileOutputStream(newPDFPath+"\\"+fileName+ ".pdf");
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//遍历数据
Map<String,Object> datemap = (Map<String,Object>)map.get("dataMap");
// 添加所创建的字体
form.addSubstitutionFont(bf);
for(String key : datemap.keySet()){
String value = datemap.get(key).toString();
form.setField(key,value);
}
//遍历条码字段
Map<String,Object> barcodeFields=(Map<String, Object>) map.get("barcodeFields");
for (Map.Entry<String, Object> entry : barcodeFields.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// 获取属性的类型
if(value != null && form.getField(key) != null){
//获取位置(左上右下)
FieldPosition fieldPosition = form.getFieldPositions(key).get(0);
//绘制条码
Barcode128 barcode128 = new Barcode128();
//字号
barcode128.setSize(10);
//条码高度
barcode128.setBarHeight(35);
//条码与数字间距
barcode128.setBaseline(20);
//条码下方的值
barcode128.setCode(value.toString());
barcode128.setStartStopText(false);
barcode128.setExtended(true);
//条形码扫描的值
barcode128.setAltText(value.toString());
//绘制在第一页
PdfContentByte cb = stamper.getOverContent(number);
//生成条码图片
Image image128 = barcode128.createImageWithBarcode(cb, null, null);
//左边距(居中处理)
float marginLeft = (fieldPosition.position.getRight() - fieldPosition.position.getLeft() - image128.getWidth()) / 2;
//条码位置
image128.setAbsolutePosition(fieldPosition.position.getLeft() + marginLeft, fieldPosition.position.getBottom());
//加入条码
cb.addImage(image128);
}
}
// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
stamper.setFormFlattening(false);
stamper.close();
out.write(bos.toByteArray());
out.flush();
out.close();
bos.close();
}
/**
* 根据规则截取pdf
* @param sourceFile 源文件路径 (D:\**\数据中心\aaa.pdf)
* @param targetFile 生成后文件路径(D:\**\数据中心\aaa_1.pdf)
* @param ranges 复制规则 "1-7"表示复制1到7页、"8-"表示复制从第八页之后到文档末尾
* @throws Exception
*/
public static void copyPdf(String sourceFile ,String targetFile, String ranges)throws Exception{
PdfReader pdfReader = new PdfReader(sourceFile);
PdfStamper pdfStamper = new PdfStamper(pdfReader , new FileOutputStream(targetFile));
pdfReader.selectPages(ranges);
pdfStamper.setFormFlattening(false);
pdfStamper.close();
}
/**
* 将pdf文件按页拆分成多个文件
* @param sourceFile (列:"D:\\**\\数据中心\\aaa.pdf")
* @param newFilePathName (列:"D:\\**\\数据中心\\aaa" 注:此处不需要带文件后缀)
* @throws Exception
*/
public static void copyPdfAll(String sourceFile ,String newFilePathName)throws Exception{
FileOutputStream fout = null;
File file = null;
PdfCopy copy = null;
PdfReader pdfReader = new PdfReader(sourceFile);
int number = pdfReader.getNumberOfPages();
for (int i = 1; i <= number; i++){
Document document = new Document(pdfReader.getPageSize(i));
file = new File(newFilePathName+"_"+i+ “。pdf”);
if(!file.getParentFile().exists()&&!file.getParentFile().isDirectory()){
file.getParentFile().mkdirs();
}
copy = new PdfCopy(document, new FileOutputStream(file));
document.open();
document.newPage();
PdfImportedPage page = copy.getImportedPage(pdfReader, i);
copy.addPage(page);
document.close();
}
}
public static void main(String[] args) throws Exception {
copyPdfAll("D:\\**\\数据中心\\aaa.pdf","D:\\**\\数据中心test\\aaa");
}
/*public static void main(String[] args) throws Exception {
copyPdf("D:\\**\\数据中心\\aaa.pdf","D:\\**\\数据中心\\aaa_1.pdf","1-1");
}*/
/*public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>();
//表单数据
Map<String, String> dataMap = new HashMap<String, String>();
dataMap.put("name", "德玛11");
dataMap.put("sex", "男");
dataMap.put("toggle_4", "On");
dataMap.put("number", "20190809001");
dataMap.put("date", "2019-08-09");
dataMap.put("detention_house", "aaa)");
//条形码数据
HashMap<Object, Object> barcodeFields = new HashMap<>();
barcodeFields.put("Barcode1","11111");
map.put("dataMap", dataMap);
map.put("barcodeFields", barcodeFields);
// 模板路径
String templatePath = "src\\main\\resources\\templates\\aaa.pdf";
// 生成的新文件路径
String newPDFPath = "D:\\fileUpload\\222\\333";
String fileName = "aaa";
//执行
pdfout(templatePath,newPDFPath,2,fileName,map);
}*/
}
好了就这样了,整理了很多文档写的一个工具类,有帮助帮忙点个赞