Table Cell Fitting Page : Table Cell Size « PDF RTF « Java






Table Cell Fitting Page

Table Cell Fitting Page
import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class TableCellFitPagePDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

    try {
      PdfWriter.getInstance(document, new FileOutputStream("TableCellFitPagePDF.pdf"));
      document.open();

      Table datatable = new Table(3);
      int headerwidths[] = {10, 24, 12};
      datatable.setWidths(headerwidths);
      datatable.setWidth(46);
      datatable.setPadding(3);

      Cell cell = new Cell(new Phrase("title", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.setLeading(30);
      cell.setColspan(3);
      cell.setBorder(Rectangle.NO_BORDER);
      cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
      datatable.addCell(cell);

      datatable.setDefaultCellBorderWidth(2);
      datatable.setDefaultHorizontalAlignment(1);
      datatable.addCell("Header 1");
      datatable.addCell("Header \n 2");
      datatable.addCell("Header 3");
      datatable.endHeaders();
      datatable.setDefaultCellBorderWidth(1);

      for(int i=0;i<200;i++){
        datatable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
        datatable.addCell("1");
        datatable.addCell("2");
        datatable.addCell("3");
      }
      datatable.setCellsFitPage(true);
      document.add(datatable);
    } catch (Exception e) {
      e.printStackTrace();
    }
    document.close();
  }
}
           
       








itext.zip( 1,748 k)

Related examples in the same category

1.Specific Cell PositionSpecific Cell Position
2.Specific Cell with Different WidthSpecific Cell with Different Width
3.Locking table Cell WidthsLocking table Cell Widths
4.Cell Fixed HeightCell Fixed Height
5.Cell Minimum HeightCell Minimum Height
6.Cell Heights Warp And NoWrapCell Heights Warp And NoWrap
7.Cells Fit PageCells Fit Page
8.Setting Cell WidthsSetting Cell Widths
9.Changing Cell WidthsChanging Cell Widths
10.Table Cell HeightTable Cell Height