I have created PDF with nested tables,it has two two rows and second row has two cols.For some reason page is breaking intwo multiple pages with blank page. Below example is working..... please see the output.I am not understanding why first page is displaying as BLANK.
Please help me .. i could not able to attach pdf file.
I used : IText.2-1-7.jar
package com.ak.pdf;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author me
*
*/
public class TestPDF {
/**
*
*/
public void generatePDFTable(){
try {
OutputStream file = new FileOutputStream(new File("D:\\anil\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Font fonts = FontFactory.getFont(FontFactory.COURIER, 10,
Font.NORMAL);
Font boldFont = FontFactory.getFont(FontFactory.COURIER, 10,
Font.BOLD);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(85f);
PdfPCell cell;
cell = new PdfPCell(new Paragraph("ABC Company MS"));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderColor(Color.GREEN);
cell.setColspan(2);
table.addCell(cell);
PdfPTable nestedTable = new PdfPTable(1);
PdfPTable nestedTable1 = new PdfPTable(1);
cell = new PdfPCell(new Paragraph("\nREB OFFER\n", boldFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
fonts));
cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIE D);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Rebate Amount: $"
+ "bbbbbbbbbbbbbbbbbbb", fonts));
cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIE D);
// cell.setBorder(0);
// nestedTable.addCell(cell);
String dots = "....................";
cell = new PdfPCell(new Paragraph("\nREBATE FORM\n", boldFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nName: " + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nAddress:" + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nCity: " + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nState: " + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nZip: " + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nEmail: " + dots, fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nStart Date: "
+ "01/02/2010", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("End Date: "
+ "01/02/2010", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Postmark Date: "
+ "01/02/2010", fonts));
cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIE D);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Received By Date: "
+ "01/02/2010", fonts));
cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIE D);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nRED REQUIREMENTS\n",
boldFont));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("ccccccccccccccccccccccccccccc" +
"ccccccccccccccccccccccccccccccccccccccccccccc c" +
"ccccccccccccccccccccccccccccccccccccccccccccccccc ccccc" +
"ccccccccccccccccccccccccccccccccccccccccccccccccc " +
"cccccccccccccccccccccccccccccccccccccdf" +
"gggggggggggggggggggggggggggggggjfdshfgggggggg " +
"ggggggggggggggggggggggggggggggggggggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg " +
"ggggggggggggggggggggggggggggggggggggggggggggggggg g" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg gg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg gggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg ggggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg gggggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg ggggggggggg" +
"ggggggggggggggggggggggggggggggggggggjshfsdfkggdfg d" +
"gdfgdnfgkmdnfgndsfg" +
"dfgdfsgbjdgkldflgd" +
"gdfgbjdflkjffffffffffffffffffffff" +
"ggggggggggggggggggggggggg" +
"ggggggggggggggggggggggggggggggggggggggggggggggggg ggggggggggggg" +
"fffffffffffffffffffffffffffffffffffffffffffffffff fffffffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffff fffffff" +
"f_______________________________________________f ff" +
"_________________________________________________ _dsfsdfs" +
"fdfffffffffffffffffffffffffffffffffffffffffffffff fffffffffffff" +
"ffffffffffffffffffffffffffffffff fffffffffffffffffffffffffff" +
" fdgd" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"fffffffffffffffffffffffffffffffffffffffffffffffff fffffffffff" +
"fffffffffffffffffffffffffffffffffffffffffffffffff ffffffEND",
fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//cell.setBorder(0);
//cell.setBackgroundColor(new Color(0xFF, 0x00, 0x00));
//nestedTable.getDefaultCell().setRowspan(1);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nCHECK REBATE STATUS:", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
// nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nCHECK REBATE STATUS:"
+ "hdfhsfjsdffsfsdf" + "\n" + "efrewkfljsdlkfnsdf"
+ "\n" + "9013403019", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
cell = new PdfPCell(new Paragraph("\nMail All Required Materials:"
+ "hhhhhhhhhhhh" + "\n"
+ "ggggggggggggggg" + "\n"
+ "vcvvvvvvvvvvvvvvvvv" + ","
+ "rrrrrrrrrrrrrrrrr" + " "
+ "mmmmmmmmmmmmmmmmmmmmmmm", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setBorder(0);
nestedTable.addCell(cell);
table.addCell(nestedTable);
cell = new PdfPCell(new Paragraph("REB RULES:", boldFont));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//cell.setBorder(0);
cell.setBorder(Rectangle.TOP);
nestedTable1.addCell(cell);
cell = new PdfPCell(new Paragraph("efjjdnffffffffffff" +
"fgdfgsfdg" +
"gdfgdfsgggggggggggggggggggggggg" +
"gdfgggggggggggggggggggggggggg" +
"fdgggggggggggggggggg" +
"fdgdfggggggggggggggggggggggggggggg" +
"ggggggggggggggggggggggggggggggg" +
"ggggggggggggggggggggggggggggg" +
"gggggggggggggggggggggggggggggggffffffffffffff ff" +
""
+ "\n\nRebate sponsored by " + "CARDONE"
+ "products", fonts));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(0);
nestedTable1.addCell(cell);
table.addCell(nestedTable1);
document.add(table);
System.out.println("Done");
document.close();
file.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestPDF obj = new TestPDF();
obj.generatePDFTable();
}
}