IT Certificaions

Google Cloud Platform

Monday, October 17, 2011

Split cell if data exceeds in itext pdf API

When we insert a big data into a cell then it inserts a page break and goes to next page automatically....

To solve this we can use  setSplitLate(false) method on table to be created in itext pdf doc.

See the complete code here......


package com.example;


import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


import java.io.FileOutputStream;


public class PdfTableExample {
  public static void main(String[] args) {
 System.out.println("Going to create pdf...");
    Document document = new Document();


    try {
      PdfWriter.getInstance(document, 
    new FileOutputStream("C://Test.pdf"));


      document.open();


      PdfPTable table = new PdfPTable(1); //1 column.
    
     
      String text="Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description "+
      "Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description "+"Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description "+
      "This is a detailed description "+"Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description "+
      "This is a detailed description "+"Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description "+"This is a detailed description "+"Test summary::: bciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozxdfhgdfhjdfjfgfgjo[fkphj.fghjog,ghopdfhgphkifgcvbkjcvlbcophkdf dfopidfhdfkohmgfhoibngfhiofhjpdfghpodfhkdfmhdfkhdopfb d98fjbo sdfopgh]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmbxcvboixzcjbocvbzxcvbizxjvbiozx]cblozxjiboz cb" +
      " xscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx bxbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzx blzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb" +
      "xcvboixzcjbocvb zxcvbizxjvbiozx]cblozxjiboz cbxscvxukcunbx zxizucvhix czuivh xcvzxblzxckbnx b xbzxcnvbzcbzbciovjbozxcmbzxmb xcvboixzcjbocvb " +
      "This is a detailed description ";
      
      PdfPCell outerTableCell1 = new PdfPCell(new Paragraph("Outer Table :Cell 1"));
      outerTableCell1.setPaddingLeft(15f);
      outerTableCell1.setPaddingRight(20f);
      outerTableCell1.setBorder(0);
 
      PdfPCell outerTableCell2 = new PdfPCell(new Paragraph("Outer Table :Cell 2"));
      outerTableCell2.setPaddingLeft(15f);
      outerTableCell2.setPaddingRight(20f);
 outerTableCell2.setBorder(0);
 
      PdfPCell descCell = new PdfPCell(new Paragraph(new Paragraph(text)));
      descCell.setPaddingLeft(15f);
 descCell.setPaddingRight(20f);
 descCell.setNoWrap(false);
 
 System.out.println("Max height of this cell:"+descCell.getMaxHeight());
 descCell.setBorder(0);
     
      table.addCell(outerTableCell1);
      table.addCell(outerTableCell2);
     table.addCell(descCell);
 
 table.setSplitLate(false);
      document.add(table);


      document.close();
      System.out.println("Completed:See File");


    } catch(Exception e){
    System.out.println("Exception::"+e.getMessage());
      e.printStackTrace();
    }
  }
}