如何在 java springmvc 中的 pdfbox 中创建 table

how to create table in pdfbox in java springmvc

我想在 pdf 页面上创建一个 table,我已经做到了,但我只需要 table.[=15= 中的骨架和 table-header ]

提前致谢

期望:我需要这样的东西

现实:原来是这样的

创建代码 table:

public static PDPageContentStream tabledetails(PDDocument document, PDPage page,PDPageContentStream contentStream, ReceiptDetails basicdetails,List<itemdetails> details) 
            throws IOException {
        contentStream.beginText();
        float yTableStart = PDRectangle.A4.getHeight() - 220;
        float tableWidth = PDRectangle.A4.getWidth() - 150;
        BaseTable table = new BaseTable(yTableStart, yTableStart, 220, tableWidth, 0, document, page, true, true);
        Row<PDPage> hRow = table.createRow(20f);
        Cell<PDPage> cell = null;
        hRow.createCell(8, "SI No").setFont(PDType1Font.HELVETICA);
        hRow.createCell(57, "Description of Goods").setFont(PDType1Font.HELVETICA);
        hRow.createCell(18, "Code").setFont(PDType1Font.HELVETICA);
        hRow.createCell(13, "Quantity").setFont(PDType1Font.HELVETICA);
        hRow.createCell(20, "Rate").setFont(PDType1Font.HELVETICA);
        hRow.createCell(20, "Amount").setFont(PDType1Font.HELVETICA);
        table.addHeaderRow(hRow);
        for(itemdetails details1 :details){
            Row<PDPage> row = table.createRow(27);
            cell = row.createCell(8, details1.getLineNumber());
            cell = row.createCell(57,details1.getItemName());
            cell = row.createCell(18, details1.getItemCode());
            cell = row.createCell(13, details1.getQuantity());
            cell = row.createCell(20,String.valueOf(details1.getProductAmount()));
            cell = row.createCell(20, String.valueOf(details1.getAmount())); 
        }
        table.draw();
        contentStream.endText();  
        return contentStream;
    }

尝试以下解决方案,

使用 setBottomBorderStyle(new LineStyle(color, width))

为每个单元格设置底部边框样式

表格详细信息方法

Cell<PDPage> hRowCellOne = hRow.createCell(8, "SI No");
hRowCellOne.setFont(PDType1Font.HELVETICA);
hRowCellOne.setBottomBorderStyle(new LineStyle(Color.white, 0));
Cell<PDPage> hRowCellTwo = hRow.createCell(57, "Description of Goods");
hRowCellTwo.setFont(PDType1Font.HELVETICA);
hRowCellTwo.setBottomBorderStyle(new LineStyle(Color.white, 0));
Cell<PDPage> hRowCellThree = hRow.createCell(18, "Code");
hRowCellThree.setFont(PDType1Font.HELVETICA);
hRowCellThree.setBottomBorderStyle(new LineStyle(Color.white, 0));
Cell<PDPage> hRowCellFour = hRow.createCell(13, "Quantity");
hRowCellFour.setFont(PDType1Font.HELVETICA);
hRowCellFour.setBottomBorderStyle(new LineStyle(Color.white, 0));
Cell<PDPage> hRowCellFive = hRow.createCell(20, "Rate");
hRowCellFive.setFont(PDType1Font.HELVETICA);
hRowCellFive.setBottomBorderStyle(new LineStyle(Color.white, 0));
Cell<PDPage> hRowCellSix = hRow.createCell(20, "Amount");
hRowCellSix.setFont(PDType1Font.HELVETICA);
hRowCellSix.setBottomBorderStyle(new LineStyle(Color.white, 0));
table.addHeaderRow(hRow);

for (int i = 0; i < details.size(); i++) {
    Row<PDPage> row = table.createRow(27);
    cell = row.createCell(8, details1.getLineNumber());
    setCellStyle(i, details.size(), cell);
    cell = row.createCell(57, details1.getItemName());
    setCellStyle(i, details.size(), cell);
    cell = row.createCell(18, details1.getItemCode());
    setCellStyle(i, details.size(), cell);
    cell = row.createCell(13, details1.getQuantity());
    setCellStyle(i, details.size(), cell);
    cell = row.createCell(20, String.valueOf(details1.getProductAmount()));
    setCellStyle(i, details.size(), cell);
    cell = row.createCell(20, String.valueOf(details1.getAmount()));
    setCellStyle(i, details.size(), cell);
}

setCellStyle 方法

public void setCellStyle(int i, int length, Cell cell){
    if(i != (length-1)){ // set default bottom border style to last row
        cell.setBottomBorderStyle(new LineStyle(Color.white, 0));
    }
}

捕获示例输出,