Search code examples
androidarraylistitextandroid-sqlite

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


I have two tables in database. By uniting both I get a new table. Now I want to show this table's values in a pdf page but its showing only a single row. How can I show the whole tables values in the PdfTable. I am using iText Library here. Database method is here :

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;
}

here is the pdf table code:

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();
            }

Solution

  • You add the table and close the document inside the for loop at its end. I.e. you do that after adding the first row only.

    To fix this move those instructions outside the loop, i.e. change this

    for (PSummaryModel pSummaryModel : listOfSummaryModel) {
    
        PdfPCell cell = null;
        cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
        ...
        tables.addCell(cell);
    
        document.add(tables);
        document.close();
    }
    

    to this:

    for (PSummaryModel pSummaryModel : listOfSummaryModel) {
    
        PdfPCell cell = null;
        cell = new PdfPCell(Phrase.getInstance(pSummaryModel.getP_title()));
        ...
        tables.addCell(cell);
    }
    
    document.add(tables);
    document.close();