如何使用 iTextpdf 在 pdf 页面中显示整个列表?

How to show an whole list in pdf page using iTextpdf?

我在数据库中有两个 table。通过结合两者,我得到了一个新的 table。现在我想在 pdf 页面中显示此 table 的值,但它只显示一行。如何在 PdfTable 中显示整个 tables 值。我在这里使用 iText 库。数据库方法在这里:

public List<PSummaryModel> testMethodSummary(){
    String ps = "Not Found";
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "select p_name, sum(i_amount) i_amount, sum(e_amount) e_amount, sum(i_amount)-sum(e_amount) profit " +
            "from (" +
            "select sr, date, project_id, expense_id expense_id, 0 i_amount, e_amount e_amount, description " +
            "from expense_voucher" +
            " union all " +
            "select sr, date, project_id, income_id income_id, i_amount i_amount, 0 e_amount, description " +
            "from income_voucher) v, projects p " +
            "where v.project_id=p.p_id " +
            "group by p_name ";
    Cursor cursor = db.rawQuery(query, null);
    List<PSummaryModel> summaryModelList = new ArrayList<>();
    while (cursor.moveToNext()){
        PSummaryModel pSummaryModel = new PSummaryModel(cursor.getString(0), cursor.getLong(1), cursor.getLong(2), cursor.getLong(3));
        summaryModelList.add(pSummaryModel);
    }
    return summaryModelList;
}

这里是 pdf table 代码:

listOfSummaryModel = databaseHelper.testMethodSummary();
            for (PSummaryModel pSummaryModel : listOfSummaryModel) {

                PdfPCell cell = null;
                cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getTotalIncome())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getTotalExpense())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                cell = new PdfPCell(Phrase.getInstance(String.valueOf(pSummaryModel.getProfit())));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                tables.addCell(cell);

                document.add(tables);
                document.close();
            }

您添加 table 并在其末尾的 for 循环内关闭文档。 IE。您只在添加第一行后执行此操作。

要解决此问题,请将这些指令移出循环,即更改此

for (PSummaryModel pSummaryModel : listOfSummaryModel) {

    PdfPCell cell = null;
    cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
    ...
    tables.addCell(cell);

    document.add(tables);
    document.close();
}

对此:

for (PSummaryModel pSummaryModel : listOfSummaryModel) {

    PdfPCell cell = null;
    cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
    ...
    tables.addCell(cell);
}

document.add(tables);
document.close();