java生成pdf合同

背景:业务需求要根据pdf合同模版生成pdf最终合同,java生成pdf详细步骤如下。

1:pom引入itextpdf

        <!--PDF-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>

2:测试代码示例

   @Test
   public void testGenerate() throws IOException, DocumentException {
       String path = "C:\\Users\\Administrator\\Desktop\\TC\\contract_template.pdf";
       String out = "D:/contract_juren2.pdf";
       PdfReader reader = new PdfReader(path);
       FileOutputStream fos = new FileOutputStream(new File(out));

       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       PdfStamper ps = new PdfStamper(reader, bos);

       AcroFields s = ps.getAcroFields();

       BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//BaseFont.ge(, Charset.defaultCharset().name(), true);
       bf.setFontDescriptor(BaseFont.FONT_WEIGHT,12f);

       s.setField("contractNumber", "STD20210618111222");
       s.setFieldProperty("contractNumber","textfont",bf,null);

       s.setField("merchantName", "北京市朝阳区巨人教育文化培训学校");
       s.setFieldProperty("merchantName","textfont",bf,null);

       s.setField("merchantAddress", "北京市朝阳区旺角广场4层南侧汇只学校");
       s.setFieldProperty("merchantAddress","textfont",bf,null);
       s.setField("contact", "刘德华");
       s.setFieldProperty("contact","textfont",bf,null);

       s.setField("mobile","18611112222");
       s.setFieldProperty("mobile","textfont",bf,null);

       ps.setFormFlattening(true);
       ps.close();

       Document doc = new Document();

       PdfCopy copy = new PdfCopy(doc, fos);
       doc.open();

       for(int i=1;i<10;i++){
           PdfImportedPage importPage = copy
                   .getImportedPage(new PdfReader(bos.toByteArray()), i);
           copy.addPage(importPage);
       }
       doc.close();
       log.info("生成成功");
   }

   /**
    * 获取pdf表单属性name
    * @throws Exception
    */
   @Test
   public void testPdf() throws Exception {
       String path = "C:\\Users\\Administrator\\Desktop\\TC\\contract_template2.pdf";
       PdfReader reader = new PdfReader(path);
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       PdfStamper ps = new PdfStamper(reader, bos);
       AcroFields form = ps.getAcroFields();
       java.util.Iterator<String> it = form.getFields().keySet().iterator();
       while (it.hasNext()) {
           String name = it.next();
           System.out.println(name);
       }
   }

3:设置自定义字体

String fontPath = "C:\\Windows\\Fonts\\msyh.ttf";
BaseFont baseFont1 = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font yahei12 = new Font(baseFont1, 12f);

所需模版、软件、自定义字体下载:
链接:https://pan.baidu.com/s/1UGEOO4aWEkbRFKh6g4wPrw
提取码:7fxg

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值