javapdfitextpdfptable

Data not showing when using PdfPTable in Itext Report


I am building an itext report with nested PdfPTables. I have been able to verify that the data is getting from the database to my data objects, but I have not been able to get that data to show up in the body of the report. The Header, which I had directly copied from a previous (functioning) report is able to add the rows, but all I get for my trouble in the body is a single line (not a single line of data, a single line as if drawn on the report itself).

I am hopeful that I am merely forgetting something simple in the middle. I will show a few of my methods:

public void makePDF(String jsonData, Document document, PdfWriter writer)
    {
        this.beginNewLandscapePage(document, writer);

        try
        {
            jsonData = DBConnect.PrintReport(CommonConstants.reportAPI,this.getParams());

            JSONObject  json                = (JSONObject) getParser().parse(jsonData);
            //System.out.println("json = "+json);
            JSONObject  reportHeaderObject  = (JSONObject) (json.get(CommonConstants.reportHeaderObjectField));
            JSONObject  reportPayloadObject = (JSONObject) (json.get(CommonConstants.reportPayloadObjectField));
            JSONArray   reportData          = (JSONArray) (reportPayloadObject.get(CommonConstants.reportDataArrayField));
            JSONObject  reportTotals        = (JSONObject) (reportPayloadObject.get(CommonConstants.reportTotalsObjectField));

            PdfPTable   mainTable = new PdfPTable(1);
            mainTable.setWidthPercentage(100);
            mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

            this.buildHeader(reportHeaderObject,writer);

            boolean showSubTotalLabel;
            try
            {
                showSubTotalLabel = this.getIsSubTotaled();
            }//try
            catch (NullPointerException ne)
            {
                showSubTotalLabel = false;
            }//catch (NullPointerException)
            String  sortStub = null;
            if(showSubTotalLabel)
            {
                sortStub = Utils.getJSONAsString(reportPayloadObject, CommonConstants.sortStubField);
            }//if(showSubTotalLabel)

            for (int i = 0; i < reportData.size(); i++)
            {
                JSONObject row = (JSONObject) reportData.get(i);
        mainTable.addCell(this.buildRow(row,showSubTotalLabel,sortStub,false));
            }//for (int i = 0; i < dedInfo.size(); i++)

mainTable.addCell(this.buildRow(reportTotals,showSubTotalLabel,sortStub,true));

            document.add(mainTable);
        }//try
        catch (ParseException e)
        {
            e.printStackTrace();
        }//catch (ParseException)
        catch (DocumentException e)
        {
            e.printStackTrace();
        }//catch (DocumentException)

    }//makePDF(String, Document, PdfWriter)


public PdfPCell buildRow(JSONObject row,boolean showSubTotalLabel,String sortStub,boolean isGrandTotal)
    {
        PdfPTable           innerRowTable   = new PdfPTable(this.getInnerRowTableWidths());
        PdfPTable           outerRowTable   = new PdfPTable(this.getOuterRowTableWidths());
        PdfPTable           resultTable     = new PdfPTable(1);
        PdfPTable           dedColumn;
        PdfPTable           leftColumn;
        PdfPTable           payColumn;
        PdfPTable           rightColumn;
        PdfPTable           taxColumn;
        PdfPCell            result;
        PayrollRegisterData rowData2;
        int                 numRows = 5;    //Number of rows in the right column, minus the two matched by the totals row and header row

        if(showSubTotalLabel)
        {
            resultTable.addCell(this.addSubTotalHeader(row,sortStub));
        }//if(showSubTotalLabel)

        boolean isTotal = true;
        try
        {
            JSONObject totalObject = (JSONObject)row.get(CommonConstants.totalsObjectField); //test only to force the NullPointerException
        }//try
        catch(NullPointerException npe)
        {
            isTotal = false;
        }//catch(NullPointerException)

        if (isTotal)
        {
            PayrollRegisterVoucherData  rowData = new PayrollRegisterVoucherData(row);

            showSubTotalLabel   = false;
            rowData2            = rowData;
            leftColumn          = this.buildLeftColumnVoucher(rowData);
            rightColumn         = this.buildRightColumnVoucher(rowData);

        }//if (isTotal)
        else
        {
            PayrollRegisterTotalData    rowData = new PayrollRegisterTotalData(row);

            numRows             = 4;    //Number of Rows in the left column of the totals minus the two for the totals and header
            showSubTotalLabel   = true;
            rowData2            = rowData;
            leftColumn          = this.buildLeftColumnTotal(rowData);
            rightColumn         = this.buildRightColumnTotal(rowData);
            if(isGrandTotal)
            {
                resultTable.addCell(this.addTotalHeader(CommonConstants.reportTotalLabel));
            }//if(isGrandTotal)
            else
            {
                resultTable.addCell(this.addTotalHeader(row,sortStub));
            }//else

        }//else

        if (rowData2.getDeductionData().length > numRows)
        {
            numRows = rowData2.getDeductionData().length;
        }//if (rowData.getDeductionData().length > numRows)

        if (rowData2.getPayData().length > numRows)
        {
            numRows = rowData2.getPayData().length;
        }//if (rowData.getPayData().length > numRows)

        if (rowData2.getTaxData().length > numRows)
        {
            numRows = rowData2.getTaxData().length;
        }//if (rowData.getTaxData().length > numRows)

        dedColumn = this.buildDedColumn(rowData2,numRows);
        payColumn = this.buildPayColumn(rowData2,numRows);
        taxColumn = this.buildTaxColumn(rowData2,numRows);

        innerRowTable.addCell(Utils.getBlankCell());
        innerRowTable.addCell(payColumn);
        innerRowTable.addCell(Utils.getBlankCell());
        innerRowTable.addCell(taxColumn);
        innerRowTable.addCell(Utils.getBlankCell());
        innerRowTable.addCell(dedColumn);
        innerRowTable.addCell(Utils.getBlankCell());

        outerRowTable.addCell(leftColumn);
        outerRowTable.addCell(innerRowTable);
        outerRowTable.addCell(rightColumn);

        resultTable.addCell(outerRowTable);
        result = new PdfPCell(resultTable);
        return result;
    }//buildRow(JSONObject,boolean,String,boolean)

I know I am a bit rusty with this, but the data is getting into the data objects successfully, and it is running without errors. It just doesn't show any data in the data section of the report. Is my problem with my mainTable, my buildRow, or do those look right and I need to look deeper into my code?


Solution

  • It turns out that the issue was not posted in the original code snippet, at least not completely. The issue was that the float[] variables I had set in the constructor were reversed for the inner and outer tables. Thus, the outer table, which I thought only wanted three columns per row, was expecting 7 columns. So, when I added it to the resultTable, it was not adding anything at all.