• 【Excel & PDF 系列】iText 库直接实现表格 PDF


    你知道的越多,你不知道的越多
    点赞再看,养成习惯
    如果您有疑问或者见解,欢迎指教:
    企鹅:869192208

    前言

    最近遇到生成 Excel 并转 PDF 的需求,磕磕碰碰总结三种方式,分别是 POI + iText 库,EasyExcel + iText 库和直接生成 PDF 表格三种方式。

    本文基于 iText 库实现,直接生成表格的 PDF 文件。

    生成表格 PDF 效果

    直接生成PDF

    引入 pom 配置
    <dependency>
        <groupId>com.itextpdfgroupId>
        <artifactId>itextpdfartifactId>
        <version>5.5.13version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    代码实现
    定义 CreateExcelToPdfModel 对象
    import lombok.Data;
    import java.util.List;
    
    /**
     * 

    生成excel表格型的pdf入参

    * @author chendw * @date 2024-02-25 10:12:51 **/
    @Data public class CreateExcelToPdfModel { //表头数据 private List<List<String>> head; //表的内容数据 private List<List<String>> dataList; //PDF表头标题 private String sheetName; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    主方法
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class TablePdfUtil {
      public static void createTablePdf(CreateExcelToPdfModel createExcelToPdfModel, String pdfFilePath) {
        try (FileOutputStream fos = new FileOutputStream(pdfFilePath)) {
          List<List<String>> headList = createExcelToPdfModel.getHead();
          List<List<String>> dataList = createExcelToPdfModel.getDataList();
          // 创建PDF文档对象
          Document document = new Document(PageSize.A2, 50, 50, 50, 50);
    
          // 创建PDF输出流
          PdfWriter writer = PdfWriter.getInstance(document, fos);
    
          // 打开PDF文档
          document.open();
    
          // 创建PDF表格对象
          PdfPTable table = new PdfPTable(headList.size());
          table.setHeaderRows(1);
    
          // 设置表格宽度
          table.setWidthPercentage(100);
    
          // 设置表格标题
          String sheetName = createExcelToPdfModel.getSheetName();
          Paragraph title = new Paragraph(sheetName, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
          title.setAlignment(Element.ALIGN_CENTER);
          document.add(title);
    
          // 添加表格标题
          for (List<String> head : headList) {
            String value = head.get(0);
            PdfPCell pdfCell = new PdfPCell(new Paragraph(value, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
            pdfCell.setBorderWidth(1f);
            pdfCell.setBorderColor(BaseColor.BLACK);
            pdfCell.setPadding(5f);
            pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(pdfCell);
          }
    
          // 添加表格内容
          for (List<String> data : dataList) {
            for (String s : data){
              PdfPCell pdfCell = new PdfPCell(new Paragraph(s, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
              pdfCell.setBorderWidth(1f);
              pdfCell.setBorderColor(BaseColor.BLACK);
              pdfCell.setPadding(5f);
              table.addCell(pdfCell);
            }
          }
    
          // 添加表格到PDF文档
          table.setSpacingBefore(20f);
          table.setSpacingAfter(20f);
          table.setKeepTogether(true);
          document.add(table);
    
          // 关闭PDF文档
          document.close();
        } catch (Exception e) {
          log.error("生成表格pdf失败:{}", e.getMessage(), e);
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    至此,就基于 iText 库直接实现了表格 PDF 的逻辑。

  • 相关阅读:
    【每日一题Day39】细分图中的可到达节点 | 图论
    HTML期末学生大作业-班级校园我的校园网页设计与实现html+css+javascript
    互联网家装驶入深水区:谁在求变,又有谁在裸泳?
    网络代理的多重应用与安全保障
    第6章 如何产生优秀的 Micro SaaS 创意
    DVWA-impossible代码审计
    测试一下禁言
    【电力运维】浅谈电力通信与泛在电力物联网技术的应用与发展
    从源码看vue(v3.2.41)中的diff原理
    javascript 文本框中的数据丢失格式,导致没有换行,显示成一行,flask不显示本地图片问题
  • 原文地址:https://blog.csdn.net/CDWLX/article/details/136359088