PDF文字填充

下载工具并安装
Adobe Acrobat DC 2018 SP

下载完成之后对一个PDF表单进行参数设置

写代码
引入jar包

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>
com.itextpdf itextpdf 5.5.13.1

写工具类
package jp.utils;

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.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PDFUtils {

/**
 * 根据模板填充pdf
 *
 * @param os       输出流
 * @param template 模板 模板制作请参考: https://99designs.com/blog/design-tutorials/pdf-business-card-template-tutorial/
 * @param params   K:文本域名称 V:填充的值
 */
public static boolean fillTemplate(OutputStream os, byte[] template, Map<String, String> params) {// 利用模板生成pdf

    boolean result = false;

    PdfStamper stamper = null;
    PdfReader reader = null;
    try {
        //  读入pdf表单
        reader = new PdfReader(template);

        //  根据表单生成一个新的pdf
        stamper = new PdfStamper(reader, os);

        //  获取pdf表单
        AcroFields form = stamper.getAcroFields();

        // 给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        form.addSubstitutionFont(bf);

        // 遍历data 给pdf表单表格赋值
        for (Iterator<Map.Entry<String, String>> it = params.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, String> entry = it.next();
            form.setField(entry.getKey(), entry.getValue());
        }

        stamper.setFormFlattening(true);

        result = true;
        //-------------------------------------------------------------
        System.out.println("===============PDF导出成功=============");
    } catch (Exception e) {
        System.out.println("===============PDF导出失败=============");
        result = false;
        e.printStackTrace();
    } finally {
        try {
            stamper.close();
            reader.close();
        } catch (Exception e) {
            result = false;
        }
    }

    return result;
}

public static void main(String[] args) throws Exception {
    try {
        String outPath = "F:/test2.pdf";
        String path = "F:/test_wrapper.pdf";

        InputStream ins = new BufferedInputStream(new FileInputStream(path));
        读取工程目录下的文件
        //InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/smmTemplate.pdf");
        FileOutputStream os = new FileOutputStream(outPath);
        byte[] byt = new byte[ins.available()];
        ins.read(byt);

        Map<String, String> data = new HashMap<>();
        data.put("username", "罗少");
        data.put("sex", "36010");

        fillTemplate(os, byt, data);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

以上是直接输入输出;


以下是可以直接在浏览器显示下载文件
OutputStream os = null;
InputStream ins = null;
try {

		String filename = "授权委托书.pdf";
		response.reset();
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gbk"), "iso8859-1"));
        os = response.getOutputStream();
        ins = new BufferedInputStream(new FileInputStream(openPath));
        byte[] byt = new byte[ins.available()];
        ins.read(byt);
        fillTemplate(os, byt, params);
        response.getOutputStream().flush();
	    response.getOutputStream().close();
	    os.close();
	    ins.close();
	} catch (Exception e) {
		try {
			os.close();
		    ins.close();
		} catch (Exception e2) {
		}
	} finally{
		try {
			os.close();
		    ins.close();
		} catch (Exception e2) {
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值