Java 向文档固定位置填入信息
准备word里面根据需要设置好用于填充信息的位置大小
下载工具 Adobe Acrobat DC
需要自己选择文本框为需要填充二维码的定好位置,然后另存为pdf
代码实现如下
package com.ygx.construction.controller;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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.AcroFields;
import com.itextpdf.text.pdf.BarcodeQRCode;
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 com.itextpdf.text.pdf.AcroFields.FieldPosition;
@Controller
@RequestMapping("/word")
public class WordController {
public static String pdfout(Map<String,Object> map) {
// 模板路径
String templatePath = "D:\\123\\word填充值.pdf";
// 生成的新文件路径
String urlStr = "D:\\123";
String newPDFPath = urlStr+"/downloadFile/"+11+".pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
//给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bf, 5, Font.NORMAL);
// 输出流
out = new FileOutputStream(newPDFPath);
// 读取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("datemap");
// 添加所创建的字体
form.addSubstitutionFont(bf);
for(String key : datemap.keySet()){
String value = datemap.get(key).toString();
form.setField(key,value);
}
Map<String,Object> qrcodeFields=(Map<String, Object>) map.get("qrcodeFields");
//遍历二维码字段
for (Map.Entry<String, Object> entry : qrcodeFields.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// 获取属性的类型
if(value != null && form.getField(key) != null){
//获取位置(左上右下)
FieldPosition fieldPosition = form.getFieldPositions(key).get(0);
//绘制二维码
float width = fieldPosition.position.getRight() - fieldPosition.position.getLeft();
BarcodeQRCode pdf417 = new BarcodeQRCode(value.toString(), (int)width, (int)width, null);
//生成二维码图像
Image image128 = pdf417.getImage();
//绘制在第一页
PdfContentByte cb = stamper.getOverContent(1);
//左边距(居中处理)
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(true);
stamper.close();
Document doc = new Document();
Font font = new Font(bf, 20);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
String downFileUrl = "";
File importFile = new File(newPDFPath);
if (null != importFile && importFile.length() > 0) {
// 上传文件服务器返回填好值的pdf文件地址
// downFileUrl = FTPUpload(importFile, "pdf");
// log.info("文件路径:" + downFileUrl);
return downFileUrl;
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception{
Map<String,Object> txtFields = new HashMap<String, Object>();
txtFields.put("Text1", "张三");
txtFields.put("Text2", "男");
txtFields.put("Text3", "100");
//二维码map
Map<String,Object> qrcodeFields = new HashMap<String, Object>();
qrcodeFields.put("Text4","https://www.baidu.com");//二维码
Map<String,Object> o=new HashMap<String, Object>();
o.put("datemap",txtFields);
o.put("qrcodeFields",qrcodeFields);
pdfout(o);
}
}
结果如下