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

导出数据表格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();
            }
        }
    }
发布了74 篇原创文章 · 获赞 143 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_39380155/article/details/105730477