1 Star 2 Fork 1

匿芒 / PdfFillUtil

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

PdfFillUtil

star

介绍

  • 基于IText,实现PDF表单域数据填充及下载功能。
  • 默认使用“华文楷体”填充,也可自行指定字体,可通过指定字体扩展包路径进行扩展;支持自动匹配表单域设置的字体格式,暂不支持同名域的字体匹配。

软件架构

  IText7


使用说明

pom中引用相应的IText依赖,复制FillPdfUtil.java文件到项目中即可。

        <!-- itext7 -->
        <dependency>
          <groupId>com.itextpdf</groupId>
          <artifactId>itext7-core</artifactId>
          <version>8.0.3</version>
          <type>pom</type>
        </dependency>

  


演示

1.模板文件

image.png

2.代码操作
    // 设置填充数据
    public static Map<String, String> getData(){
        String info = "心之所向,身之所往——for my heart";
        Map<String, String> map = new HashMap<>();
        map.put("DengXian", info);//等线
        map.put("DengXian-Light", info);//等线Light
        map.put("DengXian,Bold", info);//等线粗体
        map.put("FZShuTi", info);//方正舒体
        map.put("FZYaoTi", info);//方正姚体
        map.put("FangSong", info);//仿宋
        map.put("SimHei", info);//黑体
        map.put("STCaiyun", info);//华文彩云
        map.put("STFangsong", info);//华文仿宋
        map.put("STHupo", info);//华文琥珀
        map.put("STKaiti", info);//华文楷体
        map.put("STLiti", info);//华文隶书
        map.put("STSong", info);//华文宋体
        map.put("STXihei", info);//华文细黑
        map.put("STXinwei", info);//华文新魏
        map.put("STXingkai", info);//华文行楷
        map.put("STZhongsong", info);//华文中宋
        map.put("KaiTi", info);//楷体
        map.put("LiSu", info);//隶书
        map.put("SimSun", info);//宋体
        map.put("MicrosoftYaHei", info);//微软雅黑
        map.put("MicrosoftYaHeiLight", info);//微软雅黑Light
        map.put("MicrosoftYaHei,Bold", info);//微软雅黑粗体
        map.put("NSimSun", info);//新宋体
        map.put("YouYuan", info);//幼圆
        return map;
    }
    // 执行生成
    public static void main(String[] args) {
        // 模板地址(根据实际位置修改)
        String tempPdfPath = "/template/fontsTest.pdf";
        // 填充后PDF文件的输出地址
        String pdfOutPath = "D:\\test\\字体测试样本.pdf";
        // 字体扩展包地址(根据实际位置修改)
        String extendPath = "D:\\DEV_TOOL\\workspace\\my\\pdfFillUtil\\fonts\\extend\\";
        // 参数准备
        Map<String, String> data = getData();
        // 生成(根据需要选择相应构造方法)
        FillPdfUtil fillPdfUtil = new FillPdfUtil(true, extendPath);
        fillPdfUtil.fill(tempPdfPath, pdfOutPath, data);
    }
3.填充结果

image.png


中文字体

  • 字体映射关系

    字体名称 字体文件 Itext解读别名
    等线 Deng.ttf DengXian
    等线Light Dengl.ttf DengXian-Light
    等线粗体 Dengb.ttf DengXian,Bold
    方正舒体 FZSTK.TTF FZShuTi
    方正姚体 FZYTK.TTF FZYaoTi
    仿宋 simfang.ttf FangSong
    黑体 simhei.ttf SimHei
    华文彩云 STCAIYUN.TTF STCaiyun
    华文仿宋 STFANGSO.TTF STFangsong
    华文琥珀 STHUPO.TTF STHupo
    华文楷体 STKAITI.TTF STKaiti
    华文隶书 STLITI.TTF STLiti
    华文宋体 STSONG.TTF STSong
    华文细黑 STXIHEI.TTF STXihei
    华文新魏 STXINWEI.TTF STXinwei
    华文行楷 STXINGKA.TTF STXingkai
    华文中宋 STZHONGS.TTF STZhongsong
    楷体 simkai.ttf KaiTi
    隶书 SIMLI.TTF LiSu
    宋体 simsun.ttc SimSun
    微软雅黑 msyh.ttc MicrosoftYaHei
    微软雅黑Light msyhl.ttc MicrosoftYaHeiLight
    微软雅黑粗体 msyhbd.ttc MicrosoftYaHei,Bold
    新宋体 simsun.ttc NSimSun
    幼圆 SIMYOU.TTF YouYuan
  • 自行至 C:\Windows\Fonts 选取

  • 或下载字体包,已根据PDF常用中文字体整理

  链接:https://pan.baidu.com/s/1YmFpGspDs6GRc6QMFa7Hhw
  提取码:9654
MIT License Copyright (c) 2024 justHuman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

通过IText填充PDF模板表单域,并保存或下载,支持自动匹配windows常用中文字体(需指定字体包路径) 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/nimang/pdf-fill-util.git
git@gitee.com:nimang/pdf-fill-util.git
nimang
pdf-fill-util
PdfFillUtil
master

搜索帮助