pdf表单域填充工具

pom依赖在这里的第一部分和第二部分


import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

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

/**
 * @author aaa
 * @create 2018-12-13 18:50
 */
public class PdfHandleUtil {

    private static final Logger logger = LoggerFactory.getLogger(PdfHandleUtil.class);

    /**
     * @param templatePath 文件路径
     * @param outputPath   输出路径
     * @param fieldsMap    文件填充内容
     * @return
     * @throws Exception 
     *  pdf文档填充
     */
    public static String insertTextFieldsForRealPath(String templatePath, String outputPath, Map<String, String> fieldsMap) throws Exception {

        File file = new File(templatePath);
        // 1.判断合同模板是否存在
        if (!file.isFile()) {
            if (logger.isErrorEnabled()) {
                logger.error("[{}]合同模版不存在", templatePath);
            }
            throw new RuntimeException("合同模板文件不存在");
        }
        // 2.打开pdf读取器
        PdfReader reader = new PdfReader(templatePath);
        String fileName = file.getName();

        // 3.判断文件目录是否存在,否则自动生成
        File directory = new File(outputPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        int floor = (int) Math.floor(Math.random() * 10000);
        // 4.加random防文件重名
        String[] split = fileName.split("\\.");
        fileName = split[0] + floor + "." + split[1];
        outputPath = outputPath.concat("_").concat(fileName);

        try (FileOutputStream out = new FileOutputStream(outputPath);) {
            PdfStamper stamper = new PdfStamper(reader, out);

            AcroFields fields = stamper.getAcroFields();
            //设置宋体
            // BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // fields.setFieldProperty("enterpriseCode", "textfont", bf, null);
            // 5.对表单域进行填充
            for (String name : fields.getFields().keySet()) {
                String value = fieldsMap.get(name);
                if (!ObjectUtils.isEmpty(value)) {
                    fields.setField(name, value, true);
                }
            }
            //  6.如果为false那么生成的PDF文件还能编辑,一定要设为true
            stamper.setFormFlattening(true);
            stamper.close();
            reader.close();
        } catch (Exception e) {
            logger.error("==>>pdf表单属性填充中发生异常[{}]<<==", e);
            throw new RuntimeException("pdf表单属性填充中发生异常", e);
        }

        return outputPath;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

b0b0大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值