【发布时间】:2019-08-16 14:24:13
【问题描述】:
我正在使用嵌套的 PdfPTables 构建一个 itext 报告。我已经能够验证数据是否从数据库获取到我的数据对象,但我无法让该数据显示在报告正文中。我直接从以前的(正常运行的)报告中复制的标题能够添加行,但我在正文中遇到的所有问题都是一行(不是一行数据,一行就像绘制的一样)报告本身)。
我希望我只是在中间忘记了一些简单的事情。我将展示一些我的方法:
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)
我知道我对此有点生疏,但是数据已成功进入数据对象,并且运行时没有错误。它只是没有在报告的数据部分显示任何数据。是我的 mainTable、buildRow 有问题,还是这些看起来正确,我需要深入研究我的代码?
【问题讨论】: