你知道如何用java生成表格形式的pdf文件吗?(使用itext)

46 篇文章 2 订阅
21 篇文章 1 订阅

导出数据表格PDF样式:

在这里插入图片描述
导入依赖:

   <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.5</version>
   </dependency>

源码:

   private String[][] content = new String[][]{
            {"姓名", "英文名", "年龄", "联系电话"},
            {"刘先生", "puyuma", "22","15911111111"}
    };
    
 /**
     * 创建一份普通表格的PDF文件
     *
     * @param fullFilePath 导出pdf文件后的存放地址
     * @return
     */
    public boolean toPDF(String fullFilePath) {
        Document document = new Document();
        try {
            //构建一个PDF文档输出流程
            OutputStream outputStream = new FileOutputStream(new File(fullFilePath));
            PdfWriter.getInstance(document, outputStream);
            //设置字体样式
            BaseFont baseFont = BaseFont.createFont("D:\\msyhbd.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font f8 = new Font(baseFont, 8, Font.NORMAL);
            //打开PDF文件流
            document.open();
            //创建一个N列的表格控件
            PdfPTable table = new PdfPTable(content[0].length);
            //设置表格占PDF文档100%宽度
            table.setWidthPercentage(100);
            //设置表格控件水平方向左对齐
            table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
            //创建一个表格的表头单元格
            PdfPCell pdfTableHeaderCell = new PdfPCell();
            //设置表格的表头单元格颜色
            pdfTableHeaderCell.setBackgroundColor(new Color(213, 141, 69));
            pdfTableHeaderCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            for (String tableHeaderInfo : content[0]) {
                pdfTableHeaderCell.setPhrase(new Paragraph(tableHeaderInfo, f8));
                table.addCell(pdfTableHeaderCell);
            }
            //创建一个表格的正文内容单元格
            PdfPCell pdfTableContentCell = new PdfPCell();
            pdfTableContentCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            pdfTableContentCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            //表格内容行数的填充
            for (int i = 0; i < content.length-1; i++) {
                for (String tableContentInfo : content[1]) {
                    pdfTableContentCell.setPhrase(new Paragraph(tableContentInfo, f8));
                    table.addCell(pdfTableContentCell);
                }
            }
            document.add(table);
            return true;
        } catch (FileNotFoundException de) {
            de.printStackTrace();
            System.err.println("pdf file: " + de.getMessage());
            return false;
        } catch (DocumentException de) {
            de.printStackTrace();
            System.err.println("document: " + de.getMessage());
            return false;
        } catch (IOException de) {
            de.printStackTrace();
            System.err.println("pdf font: " + de.getMessage());
            return false;
        } finally {
            //关闭PDF文档流,OutputStream文件输出流也将在PDF文档流关闭方法内部关闭
            if (document != null && document.isOpen()) {
                document.close();
            }
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值