生成表格pdf格式

1. 添加依赖

java 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.2.5</version>
        </dependency>

2.demo代码

java 复制代码
    @SneakyThrows
    public void export(List list, OutputStream outputStream) {
        List<JbMaintainPlanEntity> list = planEntityService.list(dto);
        // 创建 PDF 写入器和文档对象
        PdfWriter writer = new PdfWriter(outputStream);
        PdfDocument pdfDocument = new PdfDocument(writer);
        Document document = new Document(pdfDocument);
        //支持中文,指定字符集
        InputStream resourceAsStream = resourceLoader.getClassLoader().getResourceAsStream("front/simsun.ttf");
        byte[] bytes = IoUtil.readBytes(resourceAsStream);
        FontProgram fontProgram = FontProgramFactory.createFont(bytes);
        PdfFont font = PdfFontFactory.createFont(fontProgram);
        document.setFont(font);
        // 创建表格,并设置列数为data中的键的数量
        float[] columnWidths = {2f, 2f, 2f, 3f, 3f, 2f};
        Table table = new Table(columnWidths);
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 添加表头
        String[] headers = {"序号", "设备分类", "设备机型", "设备", "轮保日期", "备注"};
        for (String header : headers) {
            Cell cell = new Cell().add(new Paragraph(header));
            cell.setTextAlignment(TextAlignment.CENTER);
            table.addHeaderCell(cell);
        }
        Integer count = 1;
        for (Objetc data: list) {
            Map<String, Object> map = new HashMap<>();
            BeanUtil.beanToMap(data, map, false, false);
            table.addCell(new Cell().add(new Paragraph(count.toString())).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("name"))))
                    .setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("model")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("age")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("date")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(ObjectUtil.isNull(map.get("remark")) ? "" : String.valueOf(map.get("remark"))))
                    .setTextAlignment(TextAlignment.CENTER));
        }
        // 将表格添加到文档中
        document.add(table);

        // 关闭文档
        document.close();
    }

3.字符集下载

https://gitcode.com/open-source-toolkit/c17ea/overview

相关推荐
听风吹等浪起9 小时前
002:多图合并导出PDF【网页版】
计算机视觉·pdf
yangshun_cug12 小时前
Windows 11 的 WSL2 Ubuntu 22.04 安装 Seismic Unix 44R28 完整教程
windows·ubuntu·seismic unix
ManageEngine卓豪13 小时前
Windows服务账户安全治理:特权访问自动化落地指南与工具选型
windows·pam·特权访问管理·特权账号
AxureMost13 小时前
Xrecode 音频格式转换利器
windows·电脑
蝶恋舞者15 小时前
NMC-G2网络管理卡 资料官网下载流程
pdf
西西学代码16 小时前
Flutter---StreamBuilder案例
javascript·windows·flutter
jiang_changsheng17 小时前
Conda 的默认环境创建优先级。
linux·windows·python
qq_4221525717 小时前
什么时候需要拆分或合并PDF?几种方案对比
pdf
黄一一91124318 小时前
硬盘健康监测软件!绿色便携版!电脑硬盘还能用几年?CrystalDiskInfo工具帮你提前避开数据丢失
windows·电脑·硬件工程·开源软件
hu556679818 小时前
Adobe Acrobat DC 如何按页数方法拆分2个或多个pdf
pdf·pdf拆分