java实现根据pdf文件模板生成pdf文件

更新时间:2023-07-31 07:35:27 阅读: 评论:0

java实现根据pdf⽂件模板⽣成pdf⽂件⼀、如何制作pdf模板
1.⾸先创建template.doc
2.根据doc⽂件制作pdf模板
3.将doc⽂件输出为pdf
  ⽂件->输出为pdf
4.输出的pdf⽂件
5.使⽤Adobe Acrobat DC打开template.pdf
6.填充⽂字及创作表单
创作表单->添加“⽂本”域
  创作表单->编辑“表格”
*填充的字段名(⽐如对象Table的属性为name,这⾥名称后⾯⼀列的单元格填充name)
7.模板制作完毕q10辅酶的功效
⼆、java⽣成pdf⽂件
1.java代码
PdfFileBlh.java:
@SuppressWarnings("unchecked")
public void exportPdfFile(HttpServletRequest request, HttpServletRespon respon) throws IOException {  //1.获取数据
//  String jsonString = getRequestJsonString(request);
String jsonString = Parameter("jsonString");
/*String jsonString = "{\r\n" +
"  \"hospitalId\":\"14064\",\r\n" +
"  \"hospitalName\":\"揭阳市⼈民医院\",\r\n" +
"  \"name\":\"周虎\",\r\n" +
"  \"gender\":\"男\",\r\n" +
"  \"age\":\"26岁\",\r\n" +
"  \"patNo\":\"0001278568\",\r\n" +
"  \"clinic\":\"全科医学科简易门诊\",\r\n" +
"  \"bedNo\":\"16床\",\r\n" +
"  \"specimen\":\"咽拭⼦\",\r\n" +
"  \"repNo\":\"110254767\",\r\n" +
"  \"rialNo\":\"0\",\r\n" +
"  \"doctor\":\"核酸检测号\",\r\n" +
西安春节好去处"  \"applicationTime\":\"2021-02-18 15:17\",\r\n" +
"  \"advName\":\"新型冠状病毒核酸RNA检测(⾃费)\",\r\n" +
"  \"idType\":\"⾝份证\",\r\n" +
"  \"idNo\":\"EC3263698\",\r\n" +
寓意比较好的成语
"  \"dateOfCollection\":\"2021-02-18 15:30\",\r\n" +
"  \"dateOfReceipt\":\"2021-02-19 11:13\",\r\n" +
"  \"specimenCollector\":\"黄敏华\",\r\n" +
"  \"specimenReceiver\":\"钟乔华\",\r\n" +
"  \"dateOfInspection\":\"2021-02-19 11:15\",\r\n" +
"  \"dateOfReport\":\"2021-02-19 11:15\",\r\n" +
"  \"reportedBy\":\"陈舒兰\",\r\n" +
"  \"inspectedBy\":\"钟乔华\",\r\n" +
"  \"resultList\":[\r\n" +
" {\r\n" +
"  \"testItems\":\"2019新型冠状病毒核酸检测\",\r\n" +
"  \"result\":\"阴性(-)\",\r\n" +
"  \"refRange\":\"阴性(-)\",\r\n" +
"  \"method\":\"RT-PCR\"\r\n" +
"  \"method\":\"RT-PCR\"\r\n" +
" },\r\n" +
" {\r\n" +
"  \"testItems\":\"Nucleic Acid Test For SARA-Cov-2\",\r\n" +
"  \"result\":\"Negative(-)\",\r\n" +
"  \"refRange\":\"Negative(-)\",\r\n" +
"  \"method\":\"RT-PCR\"\r\n" +
" },\r\n" +
" {\r\n" +
"  \"testItems\":\"2019-nCov ORFlab基因\",\r\n" +
"  \"result\":\"阴性Negative(-)\",\r\n" +
"  \"refRange\":\"阴性Negative(-)\",\r\n" +
"  \"method\":\"RT-PCR\"\r\n" +
" },\r\n" +
" {\r\n" +
"  \"testItems\":\"2019-nCov N基因\",\r\n" +
"  \"result\":\"阴性Negative(-)\",\r\n" +
"  \"refRange\":\"阴性Negative(-)\",\r\n" +
"  \"method\":\"RT-PCR\"\r\n" +
" }\r\n" +
"  ]\r\n" +
"}";*/
System.out.println("======jsonString======"+jsonString);
if(StringUtils.isNotBlank(jsonString)) {
//2.处理数据
Map<String, Object> data = Object(jsonString, Map.class);
//3.根据医院Id获取pdf模板⽂件
String hospitalId = (String) ("hospitalId");
String hospitalName = (String) ("hospitalName");
String templatePath = Session().getServletContext().getRealPath("/WEB-INF/class/template/template_" + hospitalId + ".pdf");
String fontPath = Session().getServletContext().getRealPath("/WEB-INF/class/");
司机简历模板System.out.println("======templatePath======"+templatePath);
//4.设置响应头信息
respon.tContentType("multipart/form-data");
respon.tHeader("Content-Disposition","attachment;fileName="+new String((hospitalName + "检验报告单_").getBytes("GBK"), "ISO-8859-1") + DateUtils.form
//5.指定解析器
ServletOutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
Document doc = null;
try {
os = OutputStream();
// 6.读⼊pdf表单
reader = new PdfReader(templatePath);
// 7.根据表单⽣成⼀个新的pdf
ps = new PdfStamper(reader, os);
doc = new Document();
/
/ 8.获取pdf表单
AcroFields form = ps.getAcroFields();
// 9.给表单添加中⽂字体。不设置的话,中⽂可能⽆法显⽰
BaFont bf = ateFont(fontPath+",1", BaFont.IDENTITY_H,BaFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
// 10.获取到明细
List<Map<String,Object>> resultList = (List<Map<String, Object>>) ("resultList");
// 11.遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
String value = (String) (key);
form.tField(key, value);
}
// 12.处理检验明细
if(resultList!=null && resultList.size()>0) {
String testItems="\r\n";
String testItems="\r\n";
String result="\r\n";
String refRange="\r\n";
String method="\r\n";爱惜花
for(Map<String,Object> resultMap:resultList) {
testItems+=("testItems")+"\r\n\r\n";
result+=("result")+"\r\n\r\n";
refRange+=("refRange")+"\r\n\r\n";
method+=("method")+"\r\n\r\n";
}
form.tField("testItems", testItems);
form.tField("result", result);
form.tField("refRange", refRange);
form.tField("method", method);
}
ps.tFormFlattening(true);
ps.clo();
doc.open();
doc.clo();
System.out.println("===============PDF导出成功=============");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.clo();
} catch (Exception e) {
e.printStackTrace();
脖子右侧有痣}
}
}
}
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = Method();
// GET
if (submitMehtod.equals("GET")) {
return new QueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");            // POST
} el {
return getRequestPostStr(request);
四辰斩邪剑}
怎样煮饭}
public static byte[] getRequestPostBytes(HttpServletRequest request)
throws IOException {
int contentLength = ContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;)
{
int readlen = InputStream().read(buffer, i,
contentLength - i);
if (readlen == -1) {
break;
}
i += readlen;
}
return buffer;
}
public static String getRequestPostStr(HttpServletRequest request)
throws IOException {

本文发布于:2023-07-31 07:35:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102858.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:表单   模板   检验   处理   创作   设置
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图