首页 >pdf操作 > 内容

PDF文字填充

2023年10月11日 21:52

下载工具并安装
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) {}}


参考文章:https://blog.csdn.net/luoshao666/article/details/105051263

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时候联系我们修改或删除,在此表示感谢。

特别提醒:

1、请用户自行保存原始数据,为确保安全网站使用完即被永久销毁,如何人将无法再次获取。

2、如果上次文件较大或者涉及到复杂运算的数据,可能需要一定的时间,请耐心等待一会。

3、请按照用户协议文明上网,如果发现用户存在恶意行为,包括但不限于发布不合适言论妄图

     获取用户隐私信息等行为,网站将根据掌握的情况对用户进行限制部分行为、永久封号等处罚。

4、如果文件下载失败可能是弹出窗口被浏览器拦截,点击允许弹出即可,一般在网址栏位置设置

5、欢迎将网站推荐给其他人,网站持续更新更多功能敬请期待,收藏网站高效办公不迷路。

      



登录后回复

共有0条评论