一、准备工作:pdf文件 编辑pdf工具:Adobe Acrobat 9 Pro(打开pdf文件,点击菜单栏中菜单——>启动表单向导)编辑生成带参pdf模板
二、PDF工具类
public class PdfUtils {
private final static Logger log = LoggerFactory.getLogger(PdfUtils.class);
// 利用模板生成pdf
public static void pdfout(Map<String,Object> o,String templatePath,String newPDFPath) {
PdfReader reader;
FileOutputStream out=null;
ByteArrayOutputStream bos=null;
PdfStamper stamper;
try {
String prefixFont = "";
String os = System.getProperties().getProperty("os.name");
if (os.startsWith("win") || os.startsWith("Win")) {
prefixFont = "C:\\Windows\\Fonts" + File.separator;
} else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
}
BaseFont bf = BaseFont.createFont(prefixFont + "simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font FontChinese = new Font(bf, 5, Font.NORMAL);
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//文字类的内容处理
Map<String,String> datemap = (Map<String,String>)o.get("datemap");
form.addSubstitutionFont(bf);
for(String key : datemap.keySet()){
String value = datemap.get(key);
form.setField(key,value);
}
//图片类的内容处理
Map<String,Image> imgmap = (Map<String,Image>)o.get("imgmap");
for(String key : imgmap.keySet()) {
Image value = imgmap.get(key);
//String imgpath = value;
Image image = value;
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
//根据路径读取图片
//Image image = Image.getInstance(imgpath);
//获取图片页面
PdfContentByte under = stamper.getOverContent(pageNo);
//图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
stamper.close();
Document doc = new Document();
Font font = new Font(bf, 32);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
//form.getTotalRevisions();
int pages= stamper.getReader().getNumberOfPages();
for(int i=1;i<=pages;i++){
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
log.error("pdfout",e);
} catch (DocumentException e) {
log.error("pdfout",e);
}finally {
if(out!=null){
try{
out.close();
}catch(Exception e){
}
}
if(bos!=null){
try{
bos.close();
}catch(Exception e){
}
}
}
}
public static byte[] inputstream2Bytes(InputStream inStream) throws IOException{
byte[] in_b = null;
try{
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
in_b = swapStream.toByteArray(); //in_b为转换之后的结果
}catch(Exception e){
log.error("inputstream2Bytes",e);
}finally {
inStream.close();
}
return in_b;
}
/**
* @Description: 文件转流
*/
public static InputStream file2InputStream(File file) throws IOException{
return new FileInputStream(file);
}
/**
* 将inputStream转化为file
* @param is
* @param file 要输出的文件目录
*/
public static void inputStream2File(InputStream is, File file) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[8192];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} finally {
os.close();
is.close();
}
}
}
三、调用测试
public static void main(String[] args) {
Map<String,String> map = new HashMap();
Map<String,Image> map2 = new HashMap();
map.put("no","1234567890");
map.put("date","2018-01-02 09:11:23");
map.put("XXX_1","测试1");
map.put("XXX_2","测试2");
try{
File pic_file = new File("xxx/tmp/pic.png");
Image pic_image = Image.getInstance(inputstream2Bytes(file2InputStream(pic_file)));
map2.put("pic",pic_image);
}catch(Exception e){
}
Map<String,Object> o=new HashMap();
o.put("datemap",map);
o.put("imgmap",map2);
// 模板路径
String templatePath = "xxx/tmp/pdf模板.pdf";
// 生成的新文件路径
String newPDFPath = "xxx/tmp/new.pdf";
pdfout(o,templatePath,newPDFPath);
}