POI实现:PDF模板替换,PDF的打印导出和WORD的打印导出

1.PDF模板替换导出

  1. PDF编辑工具:Adobe Acrobat DC
  2. 模板打印代码:
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("Check".equals(type)){
            tempFilePath="WEB-INF/templates/templates/Check.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();
    }
  1. PDF模板替换JAR包概览:
<!-- 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>

2.WORD模板替换导出

  1. 代码概览:
public void createCheckIdea(HttpServletRequest request,HttpServletResponse response,@RequestParam String docId){
    //1.找到模板doc
        String path=request.getSession().getServletContext().getRealPath("/template/意见书.doc");
        //2.找到替换的模板量
     Map<String, String> entities=new HashMap<String, String>();
        entities.put("替换名称",替换数据);//替换的名称和替换内容
        HWPFDocument doc=null;
        try{
        //3.读取文档替换模板变量
          FileInputStream input=new FileInputStream(new File(path));
          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());
           }
        //4.导出doc
         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){
                e.printStackTrace();
        }
}
  1. 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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值