PDF编辑工具:Adobe Acrobat DC
具体实现的代码:
模板打印代码:
public void PDFTemplet(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map map,String type) throws Exception {
String id = (String) map.get("id");
//TODO 根据ID查询检索出来得数据,插入模板
/* 模板路径 */
String rootPath = request.getSession().getServletContext().getRealPath("/");
String tempFilePath ="";
if("prjPhaseApplyCheck".equals(type)){
tempFilePath="WEB-INF/templates/tianJintemplates/PrjPhaseApplyCheck.pdf";
} // 获取要替换参数
Map<String, String> paramData = this.getDatePdf(map,type);
//打印准备
PdfReader reader = new PdfReader(rootPath+tempFilePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
/* 读取 */
PdfStamper pdfStamper = new PdfStamper(reader, bos);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 获取字段设置对象
AcroFields s = pdfStamper.getAcroFields();
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
pdfStamper.setFormFlattening(true);
/* 需要注意的是 setField的name和命名的表单域名字要一致 */
Iterator<String> it = s.getFields().keySet().iterator();
String nameValue = "";
while (it.hasNext()) {
// 获取文本域名称
String name = it.next().toString();
nameValue = paramData.get(name);// 获取要替换的内容
// 设置文本域字体
s.setFieldProperty(name.toString(), "textfont", bf, null); // 设置字体
if("commonSuperviseNoticeInfo".equals(type)){
s.setFieldProperty(name.toString(), "textsize", (float) 13, null); // 设置字体大小
}else{
s.setFieldProperty(name.toString(), "textsize", (float) 10, null); // 设置字体大小-五号字体对应10.5字号
}
s.setField(name, nameValue);
}
pdfStamper.close();
OutputStream fos = response.getOutputStream();
response.setContentType("application/pdf");
response.setContentLength(bos.size());
fos.write(bos.toByteArray());
fos.flush();
fos.close();
bos.close();
}
运用的jar包可以用meavn库拉下来:
<!-- pdf 相关jar包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.4.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
WORD的模板替换导出:
1.找到模板
public void createCheckIdea(HttpServletRequest request, HttpServletResponse response, @RequestParam String docId) {
String path = request.getSession().getServletContext().getRealPath("/template/监督检查意见书.doc");
this.createDocTypeService.createCheckIdea(response, docId, path);
}
2.整合数据到doc中和导出文档
public void createCheckIdea(HttpServletResponse response, String docId, String srcFile) {
HWPFDocument doc = null;
doc = initialHWPFDocument(docId, srcFile,"CheckIdea");
// 导出word
this.createDocService.exportDoc(doc, response, "监督检查意见书_");
}
3.替换的数据集合插入
/**
* 初始化HWPFDocument对象
*
* @param map
* @param typeExam
* @return
*/
@SuppressWarnings("unchecked")
private HWPFDocument initialHWPFDocument(String docId, String srcFile,String docType) {
Map<String, String> entities = new HashMap<String, String>();
entities.put("替换名称",替换数据);//工程编号
return this.createDocService.replaceDoc(srcFile, entities);
}
//返回 HWPFDocument对象
public HWPFDocument replaceDoc(String srcFile, Map<String, String> entities) {
try {
//读取文档模板
FileInputStream input=new FileInputStream(new File(srcFile));
HWPFDocument doc=new HWPFDocument(input);
//读取文档内容
Range bodyRange = doc.getRange();
//替换文档内容
for (Map.Entry<String, String> entry : entities.entrySet()) {
String a=entry.getValue();
bodyRange.replaceText("${" + entry.getKey() + "}",entry.getValue());
}
return doc;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
4.//导出文档
public void exportDoc(HWPFDocument doc, HttpServletResponse response, String docTitle) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
docTitle += sdf.format(new Date()).toString() + ".doc";
response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(docTitle.getBytes("gb18030"), "ISO8859-1") + "\"");
doc.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
WORD主要运用了POI和一个阿里巴巴的JOSN包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>