cb.endText(); cb.restoreState(); document.add(Chunk.NEWLINE); table.setTotalWidth(555.0f); int sNo = 1; float tableHeight1 = table.getTotalHeight(); System.out.println(tableHeight1); int rowCount = 2; int yAxis = 570; int newPageYAxis = 0; while (i <= 10){ for (int l=1;l<=6;l++){ if(l==1){ cell = new PdfPCell(new Phrase(String.valueOf(sNo), font)); cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT); cell.setPaddingTop(4); sNo++; }else{ cell = new PdfPCell(new Phrase(value, font)); cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT); cell.setPaddingTop(4); } if(i==10 && l==5){ cell = new PdfPCell(new Phrase(value, font)); cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell.setPaddingTop(4); } if(i==10 && l==6){ cell = new PdfPCell(new Phrase(value, font)); cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell.setPaddingTop(4); } if(l==5){ referenceAmount = referenceAmount + Integer.parseInt(value); } if(l==5){ amount = amount + Integer.parseInt(value); } System.out.println(table.getTotalHeight()); if(table.getTotalHeight()<=450){ table.addCell(cell); table.writeSelectedRows(0, 6, rowCount, -1, 20, yAxis, cb); yAxis = yAxis - 15; } else { document.newPage(); table.addCell(cell); table.writeSelectedRows(0, 6, rowCount, -1, 20, yAxis, cb); newPageYAxis = newPageYAxis + 15; } } i++; rowCount++; } System.out.println(rowCount); PdfPCell cell1 = new PdfPCell(new Phrase("", font)); cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell1.setPaddingTop(4); PdfPCell cell2 = new PdfPCell(new Phrase("Total", font)); cell2.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell2.setPaddingTop(4); PdfPCell cell3 = new PdfPCell(new Phrase("", font)); cell3.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell3.setPaddingTop(4); PdfPCell cell4 = new PdfPCell(new Phrase("", font)); cell4.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell4.setPaddingTop(4); PdfPCell cell5 = new PdfPCell(new Phrase(String.valueOf(referenceAmount), font)); cell5.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell5.setPaddingTop(4); PdfPCell cell6 = new PdfPCell(new Phrase(String.valueOf(amount), font)); cell6.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); cell6.setPaddingTop(4); System.out.println(table.getLastCompletedRowIndex()); if(table.getTotalHeight()<=450){ table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); System.out.println("IF: " + table.getTotalHeight()); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); table.addCell(staticData1); table.addCell(staticData2); table.addCell(staticData3); table.addCell(staticData4); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); System.out.println("ELSE: " + table.getTotalHeight()); rowCount++; isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData1); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false) { document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData1); table.addCell(staticData2); table.addCell(staticData3); table.addCell(staticData4); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData2); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; }else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData2); table.addCell(staticData3); table.addCell(staticData4); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData3); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData3); table.addCell(staticData4); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData4); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData4); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData5); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData5); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData6); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData6); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData7); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData7); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData8); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData8); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ System.out.println("staticData9"); table.addCell(staticData9); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData9); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ System.out.println("staticData10"); table.addCell(staticData10); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; }else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); System.out.println("else staticData10"); table.addCell(staticData10); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount-1, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } if (table.getTotalHeight() <= 450){ table.addCell(staticData11); table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb); rowCount++; } else if (isPrinted==false){ document.newPage(); cb.setColorStroke(color); cb.rectangle(10,90,575,742); cb.stroke(); table.addCell(staticData11); table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb); isPrinted = true; } System.out.println("Column Index at last " + table.getLastCompletedRowIndex()); System.out.println(table.getTotalHeight()); document.close(); file.close(); System.out.println("Pdf created successfully.."); } catch (Exception e) { e.printStackTrace(); } } }