福昕pdf模板生成

素颜马尾好姑娘i 2022-12-03 02:00 302阅读 0赞

1 先通过word编写模板内容

2 通过福昕等工具将word转换为pdf

3 根据pdf模板进行表单编辑

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p6dWNoZW55Yg_size_16_color_FFFFFF_t_70

4 数据集成代码

  1. package com.cyb.utils.office;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.AcroFields;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfCopy;
  17. import com.itextpdf.text.pdf.PdfImportedPage;
  18. import com.itextpdf.text.pdf.PdfReader;
  19. import com.itextpdf.text.pdf.PdfStamper;
  20. /**
  21. * 动态生成pdf文件
  22. * https://www.cnblogs.com/wangpeng00700/p/8418594.html
  23. * @author Administrator
  24. *
  25. */
  26. public class FXPdfUtils {
  27. // 模板路径
  28. static String templatePath = "d:/data/fx/证明模板.pdf";
  29. // 生成的新文件路径
  30. static String newPDFPath = "d:/data/fx/out"+"-11"+".pdf";
  31. // 利用模板生成pdf
  32. @SuppressWarnings("unchecked")
  33. public static void pdfout(Map<String,Object> o) {
  34. PdfReader reader;
  35. FileOutputStream out;
  36. ByteArrayOutputStream bos;
  37. PdfStamper stamper;
  38. try {
  39. BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40. out = new FileOutputStream(newPDFPath);// 输出流
  41. reader = new PdfReader(templatePath);// 读取pdf模板
  42. bos = new ByteArrayOutputStream();
  43. stamper = new PdfStamper(reader, bos);
  44. AcroFields form = stamper.getAcroFields();
  45. //表单文字类的内容处理
  46. Map<String,String> datemap = (Map<String,String>)o.get("datemap");
  47. form.addSubstitutionFont(bf);
  48. for(String key : datemap.keySet()){
  49. String value = datemap.get(key);
  50. form.setField(key,value);
  51. }
  52. //表单图片类的内容处理
  53. Map<String,String> imgmap = (Map<String,String>)o.get("imgmap");
  54. for(String key : imgmap.keySet()) {
  55. String value = imgmap.get(key);
  56. String imgpath = value;
  57. int pageNo = form.getFieldPositions(key).get(0).page;
  58. Rectangle signRect = form.getFieldPositions(key).get(0).position;
  59. float x = signRect.getLeft();
  60. float y = signRect.getBottom();
  61. //根据路径读取图片
  62. Image image = Image.getInstance(imgpath);
  63. //获取图片页面
  64. PdfContentByte under = stamper.getOverContent(pageNo);
  65. //图片大小自适应
  66. image.scaleToFit(signRect.getWidth()*2, signRect.getHeight()*1);
  67. //添加图片
  68. image.setAbsolutePosition(x, y);
  69. under.addImage(image);
  70. }
  71. stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
  72. stamper.close();
  73. Document doc = new Document();
  74. PdfCopy copy = new PdfCopy(doc, out);
  75. doc.open();
  76. PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
  77. copy.addPage(importPage);
  78. doc.close();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. System.out.println(e);
  82. } catch (DocumentException e) {
  83. e.printStackTrace();
  84. System.out.println(e);
  85. }
  86. }
  87. //数字英文 汉字显示正常
  88. public static void main(String[] args) {
  89. //复选框使用
  90. //循环列表显示
  91. Map<String,Object> map = new HashMap<String, Object>();
  92. map.put("name","张三");
  93. map.put("boy","yes");
  94. map.put("girl","no");
  95. map.put("time","2018年1月1日");
  96. map.put("idcard","411522198502062563");
  97. map.put("addr", "上海市座文娱中心");
  98. map.put("a.b","上海市文娱中心a.b");
  99. Map<String,String> mapIn = new HashMap<String, String>();
  100. mapIn.put("att", "我是嵌套属性");
  101. Map<String,String> map2 = new HashMap<String, String>();
  102. map2.put("img","d:/data/fx/earth.jpg");//可以直接是text框,做印章不太合适,需要根据不同的数据进行加密
  103. Map<String,Object> o=new HashMap<String, Object>();
  104. //map.put("inner", mapIn);
  105. List<Map<String,Object>> listData = new ArrayList<>();
  106. listData.add(map);listData.add(map);listData.add(map);listData.add(map);listData.add(map);
  107. o.put("datemap",map);
  108. o.put("imgmap",map2);
  109. o.put("list", listData); //循环数据展示
  110. //电子章使用 ok
  111. pdfout(o);
  112. }
  113. public static void extractPdf(){
  114. String templatePath ="d:/data/fx/m1.pdf";;
  115. PdfReader reader;
  116. ByteArrayOutputStream bos;
  117. PdfStamper stamper;
  118. try {
  119. reader = new PdfReader(templatePath);// 读取pdf模板
  120. bos = new ByteArrayOutputStream();
  121. stamper = new PdfStamper(reader, bos);
  122. AcroFields form = stamper.getAcroFields();
  123. List<String> valueList = new ArrayList<>();
  124. List<String> nameList = new ArrayList<>();
  125. java.util.Iterator<String> it = form.getFields().keySet().iterator();
  126. while (it.hasNext()) {
  127. String name = it.next();
  128. String value = form.getField(name);
  129. valueList.add(value);
  130. nameList.add(name);
  131. }
  132. stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
  133. stamper.close();
  134. } catch (IOException e) {
  135. e.getStackTrace();
  136. } catch (DocumentException e) {
  137. e.getStackTrace();
  138. }
  139. }
  140. }

5 效果展示

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p6dWNoZW55Yg_size_16_color_FFFFFF_t_70 1

发表评论

表情:
评论列表 (有 0 条评论,302人围观)

还没有评论,来说两句吧...

相关阅读

    相关 阅读器分屏

    在阅读PDF文档的时候,经常会碰到阅读上面几页内容,和下面几页解释需要对照着一起看的情况,总是上下来回滚动鼠标很麻烦! 福昕阅读器的【分屏功能】完美解决这个问题。(备注:目前