在for循环中使用document.newpage()方法创建新页面

时间:2015-03-04 11:41:36

标签: java itextpdf

在这段代码中,我使用iText创建PDF文件。我试图通过循环ArrayList来创建多个页面。文件已创建,但数据未正确写入,并且未创建新页面。

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

我用这种方法创建PDF表格:

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

我被困在这里。提前致谢

0 个答案:

没有答案