利用iText操作PDF文件

使用背景:存在一个PDF模板,此模板预留很多信息进行填写,类似于表单。


使用工具:Adobe Acobat XI Pro(首先在Word里面写好模板,用Word转成PDF,用这玩意打开PDF,以表单形式操作PDF,他会自动识别一部分你预留的填写输入域,其余部分自行添加。同时可以编辑输入域,编辑其字体,大小,字体在输入域的位置等等)。




Java部分:

要操作PDF需要先导入iText的jar包,web项目一定要拷贝到lib文件下一份。

java代码:

        String enterpriseCode = enterpriseInfo.getEnterpriseCode();
        String randomNum = enterpriseInfo.getRandomNum();
        String chineseName = enterpriseInfo.getChineseName();
        List tempList = xinList;
        int tempListLen = tempList.size();
        String agentName = null;
        String agentIdentityCode = null;
        for (TSealInfo sealInfo : sealInfoList)
        {
            agentName = sealInfo.getAgentName();
            agentIdentityCode = sealInfo.getAgentIdentityCode();
        }

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sysDate = sdf.format(date);

        // 模板路径
        String templatePath = getSession().getServletContext()
                .getRealPath("/pdfSource/template.pdf");
        // 生成的新文件路径
        String newPDFPath = getSession().getServletContext()
                .getRealPath("/pdfSource/templateL.pdf");
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;

        try
        {
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            
            String fontPath = getSession().getServletContext()
                    .getRealPath("/file/pdfSource/simsun.ttc,1");//字体所在路径
            BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//加载字体
            Font font = new Font(bf, 12,Font.NORMAL);            
            
            java.util.Iterator<String> it = form.getFields().keySet()
                    .iterator();
            while (it.hasNext())
            {
                String name = it.next().toString();
                System.out.println(name);
            }
            form.setFieldProperty("enterpriseCode", "textfont", bf, null);//设置字体
            form.setField("enterpriseCode", enterpriseCode);
            form.setFieldProperty("randomNum", "textfont", bf, null);
            form.setField("randomNum", randomNum);
            form.setFieldProperty("chineseName", "textfont", bf, null);
            form.setField("chineseName", chineseName);
            form.setFieldProperty("tempList", "textfont", bf, null);
            form.setField("tempList", tempList.toString().substring(1, tempList.toString().length()-1));
            form.setFieldProperty("tempListLen", "textfont", bf, null);
            form.setField("tempListLen", String.valueOf(tempListLen));
            form.setFieldProperty("agentName", "textfont", bf, null);
            form.setField("agentName", agentName);
            form.setFieldProperty("agentIdentityCode", "textfont", bf, null);
            form.setField("agentIdentityCode", agentIdentityCode);
            form.setFieldProperty("sysDate", "textfont", bf, null);
            form.setField("sysDate", sysDate);

            stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
            stamper.close();
            
           
            Document doc = new Document();
         
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy
                    .getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            
            doc.close();
        }
        catch (IOException e)        
        {
            e.printStackTrace();
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }

如果需要要在PDF中操汉字,需要下载extrajars,导入字体,尝试未果,放弃。选择导入本地字体包,c:/windows/fonts/ 选择宋体复制到某文件夹下,用代码引用字体并使用,另外一定要在Acrobat中编辑字体格式为宋体,这样子才能正常操作汉字。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值