0

In this piece of code I am using iText to create a PDF file. I am trying to create multiple pages by looping over an ArrayList. The file is created but the data is not written properly and a new page is not created.

int totalPageNo = 0;
int totalRecord = dataList.size();
if (totalRecord > 0) {
    int result = totalRecord / 35;
    if ((totalRecord % 35) > 0) {
        totalPageNo = result + 1;
    }
    System.out.println("totalPageNo :  " + totalPageNo);
}   
///////////////////////////////
int beginIndex = 0;
int lastIndex = 0;
int pageNo = 1;
//PdfPTable bodyTable11 = null;
for (int i = 1; i <= totalPageNo; i++) {
    PdfPTable bodyTable11 = null;
    if (i == 1) {
        lastIndex = 35;
    } else {
        lastIndex += 36;
    }
    bodyTable11 = nestedTblPro(dataList, beginIndex, lastIndex);
    beginIndex = lastIndex + 1;
    document.add(bodyTable11);
    document.newPage();
}

I create the PDF table with this method:

public static PdfPTable nestedTblPro(ArrayList<ProjectionChartData> dataList, int beginIndex, int lastIndex) {
    System.out.println("Inside ad ----");
    PdfPTable reportTbl = new PdfPTable(9);
    reportTbl.setWidthPercentage(95);
    PdfPCell c = new PdfPCell(new Phrase("DATE",Bold));
    c.setRowspan(2);
    reportTbl.addCell(c);
    PdfPCell c1 = new PdfPCell(new Phrase("AD PLAYS",Bold));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setColspan(2);
    reportTbl.addCell(c1);
    PdfPCell c2 = new PdfPCell(new Phrase("ACTION SMS",Bold));
    c2.setHorizontalAlignment(Element.ALIGN_CENTER);
    c2.setColspan(2);
    reportTbl.addCell(c2);
    PdfPCell c3 = new PdfPCell(new Phrase("UNIQUE MSISDN",Bold));
    c3.setHorizontalAlignment(Element.ALIGN_CENTER);
    c3.setColspan(2);
    reportTbl.addCell(c3);
    PdfPCell c4 = new PdfPCell(new Phrase("CAMPAIGN COST",Bold));
    c4.setHorizontalAlignment(Element.ALIGN_CENTER);
    c4.setColspan(2);
    reportTbl.addCell(c4);
    PdfPCell c5 = new PdfPCell(new Phrase("Originally Projected",bfBold12));
    reportTbl.addCell(c5);
    PdfPCell c6 = new PdfPCell(new Phrase("Actual",bfBold12));
    reportTbl.addCell(c6);
    PdfPCell c7 = new PdfPCell(new Phrase("Originally Projected",bfBold12));
    reportTbl.addCell(c7);
    PdfPCell c8 = new PdfPCell(new Phrase("Actual",bfBold12));
    reportTbl.addCell(c8);
    PdfPCell c9 = new PdfPCell(new Phrase("Originally Projected",bfBold12));
    reportTbl.addCell(c9);
    PdfPCell c10 = new PdfPCell(new Phrase("Actual",bfBold12));
    reportTbl.addCell(c10);
    PdfPCell c11 = new PdfPCell(new Phrase("Originally Projected",bfBold12));
    reportTbl.addCell(c11);
    PdfPCell c12 = new PdfPCell(new Phrase("Actual",bfBold12));
    reportTbl.addCell(c12);
    PdfPCell c13=null;
    if(lastIndex > dataList.size()){
        lastIndex = dataList.size();
    }
    System.out.println("Begin index : " + beginIndex + " === last index : " + lastIndex + " === list size : " + dataList.size());
    for (int i = beginIndex; i < lastIndex; i++) {
        ProjectionChartData obj = dataList.get(i);
        System.out.println("-------------------------------------------------"+obj.getActionSMSStr());
        insertCell(reportTbl, c13, CommonUtilities.changeDateFormat("dd/MM/yyyy","dd MMM yyyy",obj.getProjectionDate()), "left");
        insertCell(reportTbl, c13, obj.getAdPlaysStr(), "left");
        insertCell(reportTbl, c13, obj.getActualadPlaysStr(), "left");
        insertCell(reportTbl, c13, obj.getActionSMSStr(), "left");
        insertCell(reportTbl, c13, obj.getActualactionSMSStr(), "left");
        insertCell(reportTbl, c13, obj.getUniqueMSISDNStr(), "left");
        insertCell(reportTbl, c13, obj.getActualuniqueMSISDNStr(), "left");
        insertCell(reportTbl, c13, obj.getCampaignCostStr(), "left");
        insertCell(reportTbl, c13, obj.getActualcampaignCostStr(), "left");
    }
    return reportTbl;
}

I am stuck in this. Thanks in advance

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
surendra
  • 1
  • 1
  • Could you be a bit more specific? Do you get errors? What is your input, and output + expected output? Could you also post only your relevant code? – Bono Mar 04 '15 at 12:07
  • Try to invert these calls `document.add(bodyTable11); document.newPage();` to: `document.newPage(); document.add(bodyTable11);` – Javi Mollá Mar 04 '15 at 12:35
  • I cleaned up your question and then saw that @Bono has a point: it is not clear what you're asking. Why do you think there will always be 35 records on each page? Why are you distributing the table over different pages yourself? What is the `insertCell()` method about? There are too many strange things in your code for a StackOverflow reader to be able to answer it. – Bruno Lowagie Mar 04 '15 at 14:09
  • @javier: I already tried this too. But no success. – surendra Mar 10 '15 at 05:08
  • @Bruno: I defined 35 records for each page because if I don't do like this then records shifted to next page but it overlaps the footer content of the page, so I defined this. If you other way to display table data through which I can manage the records in table please share with me. All I have to manage multiple tables one after other. – surendra Mar 10 '15 at 05:11
  • How do you add the footer content? If you do it correctly (using page events), tables distributed over different pages automatically by iText will only overlap with the footer if you defined a bottom margin that is too small to accommodate the footer. Once more we have to inform you that your code sample doesn't show us what we need to know in order to help you. – Bruno Lowagie Mar 10 '15 at 08:10
  • Am using Rectangle for footer. If I remove 'document.newPage();' then it distributes the table data to multiple pages but overlap the footer contents also. But the problem is, if I add 'document.newPage();' then it creates a corrupt PDF file. – surendra Mar 10 '15 at 08:20

0 Answers0