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

springboot itextpdf 形式导出pdf

  1. 先看效果(这里只设置了软件版本和 完成情况的勾选框)
    在这里插入图片描述
  2. 导入pom依赖
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itext-asian</artifactId>
	<version>5.2.0</version>
</dependency>
<!--itextpdf-->
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.10</version>
</dependency>
  1. 功能实现
package com.example.demotest.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;


@RestController
public class PdfController {

    /**
     * 导出pdf
     *
     * @param response
     * @return
     * @author shaolin
     */
    @PostMapping(value = {"/exportpdf"})
    public String exportPdf(HttpServletResponse response) throws IOException {
        // 指定解析器
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        String filename = "pdf/计算机编号.pdf";
 
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode(filename, StandardCharsets.UTF_8));
        OutputStream os = null;
        PdfStamper ps = null;
        PdfReader reader = null;
        try {
            os = response.getOutputStream();
            // 2 读入pdf表单
            Path path = Paths.get("pdf", "计算机编号.pdf");
            String pathing = path.toAbsolutePath().toString();
            // 输出文件路径
            reader = new PdfReader(pathing);
            // 3 根据表单生成一个新的pdf
            ps = new PdfStamper(reader, os);
            // 4 获取pdf表单

            AcroFields form = ps.getAcroFields();
            // 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
            BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//            BaseFont bf = BaseFont.createFont("SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            form.addSubstitutionFont(bf);
            // 6查询数据================================================
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("deptName", "测试部门");
            data.put("version", "1");

            // 7遍历data 给pdf表单表格赋值
            for (String key : data.keySet()) {
                form.setField(key, data.get(key).toString());
            }
            // 多选框设置,  name是名称, value 是导出值
            ps.setFormFlattening(true);
            form.setField("test", "On", true);

//            -----------------------------pdf 添加图片----------------------------------
//            通过域名获取所在页和坐标,左下角为起点
            System.out.println("pdf 添加图片");
//            String imgpath = "e:/美女.png";
//            int pageNo = form.getFieldPositions("img").get(0).page;
//            Rectangle signRect = form.getFieldPositions("img").get(0).position;
//            float x = signRect.getLeft();
//            float y = signRect.getBottom();
//            // 读图片
//            Image image = Image.getInstance(imgpath);
//            // 获取操作的页面
//            PdfContentByte under = ps.getOverContent(pageNo);
//            // 根据域的大小缩放图片
//            image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//            // 添加图片
//            image.setAbsolutePosition(x, y);
//            under.addImage(image);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
                reader.close();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
  1. 对应项目路径截图
    在这里插入图片描述

  2. 模板设置,这里使用的是adobe2024版

在这里插入图片描述

4.1 对于数字和文字的设置为文本域
在这里插入图片描述
4.2 双击截图里的文本域,出现下图所示,名称,这个名称就是你的字段名称
在这里插入图片描述
如果要显示中文,在外观这里设置宋体, 不然导出pdf 文字不显示
在这里插入图片描述

  1. 单选框功能实现
    5.1 选择复选框
    在这里插入图片描述
    5.2 设置字段名,以及字段值和展示的形式(例如框内是对勾,还是个黑点,等等)

在这里插入图片描述
在这里插入图片描述
对应代码里的(这里要设为true才行,不然框里也不显示对勾)
form.setField(“test”, “On”, true);


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

相关文章:

  • 2024/9/27刷题记录(cf1800 - 2000)
  • Redis缓存淘汰算法详解
  • VScode C语言中文乱码问题解决
  • 【HarmonyOS】组件长截屏方案
  • 【嵌入式开发】有关16head(16接口点击器)相关的资料
  • Java17-Sealed Classes(密封类)
  • 电信光猫破解记录
  • SprintBoot 中动态扩展 MongoDB 数据库字段,怎么创建实体类?
  • Axios 封装网络请求
  • 总结拓展十一:S4 HANA和ECC区别
  • 进阶 RocketMQ - 消息存储-一张图掌握核心要点
  • 数字化转型:开启未来发展新引擎
  • Vue可视化大屏模板
  • 服务器的地址如何伪装起来
  • Spring Boot 学习之路 -- 基础认知
  • 触发器对象
  • 计算机的历史,你可知道?
  • 连接池和长连接的区别和优缺点
  • 【VUE】状态管理:Pinia组件、Cookie组件
  • 基于单片机巡迹避障智能小车系统