Java通过PDF模板导出数据,adobeacrobat的PDF编辑器,itextpdf
前⾔
之前做过⼀个项⽬,通过PDF上的占位符动态地填充数据并下载,这⾥就需要⽤到adobe的软件去设置PDF的格式,并获取到当前占位符的id。
环境
adobe acrobat :pdf 编辑器;
使⽤ word导出PDF格式
打 开 adobe acrobat ⼯具;(如果提⽰错误,右键选择以管理员⽅式运⾏试试)
选择 ⽂件 -> 创建 -> 创建表单
选择刚保存的pdf⽂件,并打开;
adobe acrobat ⼯具会在表格中⾃动填充⼀些“⽂本域”,默认 “fill_” 开头;
看到有 12 个 ⽂本域,就说明模板创建成功了,直接 ctrl+s 保存下,关闭即可;
itext.jar : pdf 开发组件
<!--itextpdf jar包开始-->
<dependency>网页模板编辑器
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>l</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
java开发
@RequestMapping(value ="/downloadWord/{employeeId}", method = RequestMethod.GET)
public Result downloadWord(HttpServletResponse response,@PathVariable Long employeeId){
final BaseAccount dbBa = (employeeId).getResultData();
Date signDate = AuditTime();
Picture sign = Sign();
final String sysPath =getSispPath().replaceAll("/sharedSalaryManager/","");
Map<String, String> params =new HashMap<String, String>(){
{
put("username", Name());
put("firstPartySignDate",isNotNull(signDate)? ISO_DATE_FORMAT.format(signDate):"_____________");
put("secondPartySignDate",isNotNull(signDate)? ISO_DATE_FORMAT.format(signDate):"_____________");
put("sign",isNotNull(sign)? sysPath + RemoteRelativeUrl():"");
}
};
portTempPDF(response, params,"personAndPlatform.pdf",380.00f,360.00f);
}
public static Result exportTempPDF(HttpServletResponse response, Map<String, String> map, String templateName,float absoluteX,float absoluteY){ // 模板路径
String inRelativePath =getSispPath()+"WEB-INF/word/";
String inAbsolutePath = inRelativePath + templateName;
String inAbsolutePath = inRelativePath + templateName;
ByteArrayOutputStream bos = null;
PdfStamper ps = null;
try{
PdfReader reader =new PdfReader(inAbsolutePath);
bos =new ByteArrayOutputStream();
Font font = Font(getSispPath()+"WEB-INF/fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseFont baseFont = BaseFont();
ps =new PdfStamper(reader, bos);
AcroFields form = ps.getAcroFields();
form.addSubstitutionFont(baseFont);
for(Map.Entry<String, String> entry : Set()){
String key = Key();
String value = Value();
ains("/")&&countMatches(value,"/")>=2){
String imgPath = (Key());
int pageNo = FieldPositions(key).get(0).page;
Image image = Instance(imgPath);
PdfContentByte under = ps.getOverContent(pageNo);
image.scaleToFit(100,40);
image.setAbsolutePosition(absoluteX, absoluteY);
image.setInverted(false);
under.addImage(image);
}else{
form.setField(key, value);
}
}
ps.setFormFlattening(true);
String fileName1 = de(templateName.substring(0, templateName.indexOf("."))+ DateUtil.ISO_DATETIME_FORMAT_NONE.form at(new Date())+".pdf","UTF-8");
String headStr ="attachment; filename=\""+ fileName1 +"\"";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = OutputStream();
out.ByteArray());
out.close();
return ResultUtil.buildSuccess();
}catch(Exception e){
throw new GeneralBizException("模板PDF填充导出异常,请联系管理员:"+ e.getMessage());
}finally{
try{
if(isNotNull(ps)){
ps.close();
}
if(isNotNull(bos)){
bos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论