Search code examples
javaspring-bootspring-mvcpdfboxboxable

how to create table in pdfbox in java springmvc


I want to create a table on pdf page, I already did it, but I need only the skeleton and table-header in the table.

Thanks in advance

Expectation: I need something like this enter image description here

Reality: Actually it came like this enter image description here

code for create 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;
    }

Solution

  • try with following solution,

    set bottom border style to every cell using setBottomBorderStyle(new LineStyle(color, width))

    tabledetails method

    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 method

    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));
        }
    }
    

    capture of sample output,

    enter image description here