Search code examples
javapdfbox

values are getting overwritten in the table -PDFbox


I want to display the set of records in rows and columns. Am getting output but the thing is, it is getting overlapped. should i modify the loop can someone pls suggest.

 ArrayList<ResultRecord> Records = new ArrayList<ResultRecord>(MainRestClient.fetchResultRecords(this.savedMainLog));
 for(j=0; j<Records.size(); j++)
           {
                Row<PDPage> row4 = table.createRow(100.0f);
                Cell<PDPage> cell10 = row4.createCell(15.0f, temp.getNumber());
                Cell<PDPage> cell11 = row4.createCell(45.0f, temp.getDescription());
                Cell<PDPage> cell12 = row4.createCell(15.0f, temp.getStatus());
                Cell<PDPage> cell13 = row4.createCell(25.0f, temp.getRemarks());
                }

The below is the full code for opening a PDF file. I want to retreive set of records in the row4 in the corresponding cells. But the is over written one above the another. Expected output: IT should display one below the another. Is the overlapping reason,is it because of defining the row as row4.

 try {
                //table.draw();

                cell.setFontSize(12);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

Solution

  • First of all, you should clarify the table drawing library you use. PDFBox only is the underlying PDF library. Considering the classes used I would assume you are using Boxable on top of it.

    Furthermore, the reason why all the tables are printed over each other is that you start each table at the same position on the same page, you use

    BaseTable table = new BaseTable(yPosition, yStartNewPage,
            bottomMargin, tableWidth, margin, document, page, true, drawContent);
    

    without ever changing yPosition or page.

    To get one table after the other, you have to update yPosition and page accordingly, e.g. by using the return value of table.draw() and the state of table then, i.e. by replacing

    table.draw();
    

    by

    yPosition = table.draw();
    page = table.getCurrentPage();