在 Itext 报告中使用 PdfPTable 时不显示数据
Data not showing when using PdfPTable in Itext Report
我正在使用嵌套的 PdfPTables 构建 itext 报告。我已经能够验证数据是否从数据库获取到我的数据 objects,但我无法让该数据显示在报告的 body 中。 Header,我直接从以前的(运行中的)报告中复制的,它能够添加行,但是我在 body 中遇到的麻烦只是一行(不是一行数据,就像在报告本身上绘制的一样)。
我希望我只是忘记了中间的一些简单的东西。我将展示我的一些方法:
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)
我知道我对此有点生疏,但数据正在成功地进入数据 objects,而且 运行 没有错误。它只是不在报告的数据部分显示任何数据。是我的 mainTable、buildRow 有问题,还是它们看起来正确,我需要更深入地查看我的代码?
事实证明,问题并没有在原始代码片段中发布,至少没有完全发布。问题是我在构造函数中设置的 float[] 变量对于内部和外部 tables 是相反的。因此,我认为每行只需要三列的外部 table 需要 7 列。所以,当我将它添加到 resultTable 时,它根本没有添加任何东西。
我正在使用嵌套的 PdfPTables 构建 itext 报告。我已经能够验证数据是否从数据库获取到我的数据 objects,但我无法让该数据显示在报告的 body 中。 Header,我直接从以前的(运行中的)报告中复制的,它能够添加行,但是我在 body 中遇到的麻烦只是一行(不是一行数据,就像在报告本身上绘制的一样)。
我希望我只是忘记了中间的一些简单的东西。我将展示我的一些方法:
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)
我知道我对此有点生疏,但数据正在成功地进入数据 objects,而且 运行 没有错误。它只是不在报告的数据部分显示任何数据。是我的 mainTable、buildRow 有问题,还是它们看起来正确,我需要更深入地查看我的代码?
事实证明,问题并没有在原始代码片段中发布,至少没有完全发布。问题是我在构造函数中设置的 float[] 变量对于内部和外部 tables 是相反的。因此,我认为每行只需要三列的外部 table 需要 7 列。所以,当我将它添加到 resultTable 时,它根本没有添加任何东西。