当前位置: 首页 > article >正文

Java使用Word模板导出PDF及PDF盖章

主要步骤流程:

1、设计word模板

2、将模板转为PDF

3、使用Adobe Acrobat  设计表单

4、pdf内容填写输出

一、使用word设计模板

二、将模板转为PDF(跳过)

三、使用Adobe Acrobat  设计表单

1、使用打开pdf

2、选择工具的准备表单

3、填写文本域名称

4、如果有需要插入图片选择图片,并将外框和颜色选为无颜色

三、内容填写输出

       <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
 
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.13</version>
        </dependency>
 
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
        </dependency>
/**
     * 输出pdf
     * @param templatePath 模板
     * @param exportPath 新文件
     * @param data 文本
     * @param images 图片
     * @throws IOException 
     */
    public static void generateTempPDF(String templatePath,String exportPath,Map<String, String> data ,Map<String, String> images) throws IOException {
        PdfReader reader = null;
        PdfStamper ps = null;
        OutputStream fos = null;
        ByteArrayOutputStream bos = null;
        try {
            //模板路径templatePath
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            ps = new PdfStamper(reader, bos);
            // 使用中文字体
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            ArrayList<BaseFont> fontList = new ArrayList<>();
            fontList.add(bf);
            AcroFields fields = ps.getAcroFields();
            fields.setSubstitutionFonts(fontList);
            fillData(fields, data);//渲染
            //插入图片
            if (EmptyUtils.isNotEmpty(images)) {
                fillImage(fields, ps,images);
            }
            //必须要调用这个,否则文档不会生成的
            ps.setFormFlattening(true);
            if(ps != null){
                ps.close();
            }
            //生成pdf路径存放的路径
            fos = new FileOutputStream(exportPath);
            fos.write(bos.toByteArray());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fos!=null){
                fos.flush();
                fos.close();
            }
            if (bos != null){
                bos.close();
            }
            if(reader != null){
                reader.close();
            }
        }
    }

    private static void fillImage(AcroFields fields, PdfStamper ps, Map<String, String> images) {
        try {
            for (String key : images.keySet()) {
                String value = images.get(key);
                // 为字段赋值,注意字段名称是区分大小写的
                insertImage(fields, ps, key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 填充模板中的数据
     */
    public static void fillData(AcroFields fields, Map<String, String> data) {
        try {
            for (String key : data.keySet()) {
                String value = data.get(key);
                // 为字段赋值,注意字段名称是区分大小写的
                fields.setField(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * pdf模板插入图片
     * @param form
     * @param stamper
     * @param filedName
     * @param url
     * @return
     */
    public  static boolean insertImage(AcroFields form, PdfStamper stamper, String filedName, String url) {
        try {
            int pageNo = form.getFieldPositions(filedName).get(0).page;
            Rectangle signRect = form.getFieldPositions(filedName).get(0).position;
            float x = signRect.getLeft();
            float y = signRect.getBottom();

            Image image = Image.getInstance(url);
            // 获取操作的页面
            PdfContentByte under = stamper.getOverContent(pageNo);
            // 根据域的大小缩放图片
            image.scaleToFit(signRect.getWidth(), signRect.getHeight());
            // 添加图片
            image.setAbsolutePosition(x, y);
            under.addImage(image);
        }catch (Exception e){
            return false;
        }
        return true;
    }


http://www.kler.cn/a/428268.html

相关文章:

  • CSRF跨站请求伪造
  • IDEA关联Tomcat服务启动教程及普通JavaEE工程到Maven和Web工程启动的教程
  • [2023年12月17日]第15届蓝桥杯青少组stema选拔赛C++中高级(第二子卷、编程题(3))
  • 超标量处理器设计笔记(6)TLB缺失、替换、写入
  • 基于51单片机64位病床呼叫系统设计( proteus仿真+程序+设计报告+原理图+讲解视频)
  • MATLAB 建筑顶面面积计算(95)
  • 非常简单实用的前后端分离项目-仓库管理系统(Springboot+Vue)part 4
  • SQLCipher:SQLite加密工具的实用指南
  • Hive 的 Hook 机制 完全解析
  • 论文概览 |《Urban Analytics and City Science》2023.03 Vol.50 Issue.3
  • 【MySql】navicat连接报2013错误
  • 子矩阵最大累加和
  • selenium+python实现12306自动化抢火车票(二)
  • 调度算法(2)
  • Spring Boot性能提升:实战案例分析
  • Android WebView加载本地html文件
  • python学习笔记—1—基础环境配置和字面量
  • Figma入门-旋转效果
  • 什么是Angular?
  • MySQL-SQL语句