基于itextpdf的java读取和更新pdf表单域字段值功能

2023-12-13 05:49:26

基于itextpdf的java读取和更新pdf表单域字段值功能

执行结果为:
在这里插入图片描述

Hello World!
key=topmostSubform[0].Page1[0].qhjc[0]
key=topmostSubform[0].Page1[0].qhmc[0]
key=topmostSubform[0].Page1[0].cqzh[0]
key=topmostSubform[0].Page1[0].fm_year[0]
key=topmostSubform[0].Page1[0].nd[0]
key=topmostSubform[0].Page1[0].fm_day[0]
key=topmostSubform[0].Page1[0].fj[0]
key=topmostSubform[0].Page1[0].qt[0]
key=topmostSubform[0].Page1[0].bdcdyh[0]
key=topmostSubform[0].Page1[0].ywr[0]
key=topmostSubform[0].Page1[0].qlr[0]
key=topmostSubform[0].Page1[0].zmqlhsx[0]
key=topmostSubform[0].Page1[0].fm_month[0]
key=topmostSubform[0].Page1[0].zl[0]
key=topmostSubform[0].Page1[0].qrcode[0]
OK!
gitee.com

pom.xml中添加引用

<!-- pdf reader/writer-->
      <dependency>
          <groupId>com.itextpdf</groupId>
          <artifactId>itextpdf</artifactId>
          <version>5.5.13</version>
      </dependency>
      <!--pdf supert chinese text -->
      <dependency>
          <groupId>com.itextpdf</groupId>
          <artifactId>itext-asian</artifactId>
          <version>5.2.0</version>
      </dependency>

java代码:

package com.cwgis;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

//此类读取pdf文件中表单域,读取出字段列表  OK
public class test_pdf {

    public void updatePdf(String inputFileName, String outputFileName,Map<String, Object> data)
    {
        OutputStream os = null;
        PdfStamper ps = null;
        PdfReader reader = null;
        try
        {
            os = new FileOutputStream(new File(outputFileName));
            // 2 读入pdf表单
            reader = new PdfReader(inputFileName);
    
            // 3 根据表单生成一个新的pdf
            ps = new PdfStamper(reader, os);
            // 4 获取pdf表单
            AcroFields form = ps.getAcroFields();

            //显示表单中所有字段名称
            for(String key : form.getFields().keySet())
            {
                System.out.println("key="+key);
            }
    
            // 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
            // BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",
            // BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            // 方法二:使用iTextAsian.jar中的字体
            // BaseFont baseFont =
            // BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            //BaseFont bf = BaseFont.createFont("d:/user/80004194/desktop/test/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //form.addSubstitutionFont(bf);

            // 7遍历data 给pdf表单表格赋值
            for (String key : data.keySet()) {
                form.setField(key, data.get(key).toString());
            }
            ps.setFormFlattening(true);
            //System.out.println("===============PDF导出成功=============");
        } catch(Exception e)
        {
            //System.out.println("===============PDF导出失败=============");
            e.printStackTrace();
        } finally
        {
            try {
                ps.close();
                reader.close();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
   }

    public void updatePdf_test(String inputFileName, String outputFileName)
    {
        // 6查询数据================================================
        Map<String, Object> data = new HashMap<String, Object>();
            /*
            data.put("empName", "金星");
            data.put("empNumber", "束带结发看电视");           
            data.put("empDateOfIssue", "2019-12-3");
            data.put("empCertificateNo", "20190100833257");
            data.put("empApprovalNo_2019", "ZN-TP-CSS-20180002");
            data.put("empTrainingDate_2019", "2019-11-3");
            data.put("empCategory_2019", "初训");
            data.put("empResult_2019", "合格");
            data.put("empSignature_2019", "露西");
            data.put("answer", "张三");
            */

        data.put("zmqlhsx","zmqlhsx");
        data.put("ywr","ywr张三");
        //
        this.updatePdf(inputFileName,outputFileName,data);
    }

}

main代码:

test_pdf pdf=new test_pdf();
pdf.updatePdf_test("D:\\data\\pdf\\zszm_td.pdf","D:\\data\\pdf\\zszm_td_out.pdf");


文章来源:https://blog.csdn.net/hsg77/article/details/134954557
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。