Java Tutorial/PDF/Table Cell — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:21, 1 июня 2010

Add content to table cell

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(3);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell("the quick brown fox");
    table.addCell("the quick brown fox");
    document.add(table);
    document.close();
  }
}





Add paragraph of first line indented to a table cell

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;
    Paragraph p = new Paragraph(".");
    cell = new PdfPCell(p);
    cell.setIndent(20);
    table.addCell(cell);
    table.addCell("extra indentation (paragraph)");
    p.setFirstLineIndent(10);
    cell = new PdfPCell();
    cell.addElement(p);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Add paragraph to table cell with alignment setting

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;
    table.addCell("default alignment");
    table.addCell("paragraph alignment");
    Paragraph p1 = new Paragraph("Quick brown fox");
    Paragraph p2 = new Paragraph("jumps over");
    p2.setAlignment(Element.ALIGN_CENTER);
    Paragraph p3 = new Paragraph("the lazy dog.");
    p3.setAlignment(Element.ALIGN_RIGHT);
    cell = new PdfPCell();
    cell.addElement(p1);
    cell.addElement(p2);
    cell.addElement(p3);
    table.addCell(cell);
    table.addCell("extra indentation (cell)");
    document.add(table);
    document.close();
  }
}





Auto Fill Empty cells

import java.awt.Point;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Table table = new Table(2, 2);
    table.setAlignment(Element.ALIGN_LEFT);
    table.setAutoFillEmptyCells(true);
    table.addCell("0.0");
    table.addColumns(2);
    float[] f = { 2f, 1f, 1f, 1f };
    table.setWidths(f);
    table.addCell("2.2", new Point(2, 2));
    table.deleteColumn(2);
    document.add(table);
    document.close();
  }
}





Cell border color and background color

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
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;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(4);
    table.setWidthPercentage(100);
    PdfPCell cell;
    cell = new PdfPCell(new Paragraph("t"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("d"));
    cell.setBorder(Rectangle.TOP);
    cell.setUseBorderPadding(true);
    cell.setBorderWidthTop(5f);
    cell.setBorderColorTop(Color.cyan);
    cell.setBackgroundColor(Color.blue);
        cell.setBorder(Rectangle.BOTTOM);
    cell.setBorderColorBottom(Color.magenta);
    cell.setBorderWidthBottom(10f);
    cell.setBackgroundColor(Color.green);    
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("r"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("b"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("G:"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.25"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.5"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.75"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph(":"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("a"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("b"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("o"));
    table.addCell(cell);    
    document.add(table);
    document.close();
  }
}





Change table cell background color

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      float[] widths = { 1, 4 };
      PdfPTable table = new PdfPTable(widths);
      table.setWidthPercentage(30);
      PdfPCell cell = new PdfPCell(new Paragraph("fox"));
      cell.setBackgroundColor(Color.YELLOW);
      table.addCell(cell);
      table.addCell("asdf");
      document.add(table);
    document.close();
  }
}





Image is scaled

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image img = Image.getInstance("dog.jpg");
    PdfPTable table = new PdfPTable(1);
    table.addCell(img);
    document.add(table);
    document.close();
  }
}





implements PdfPCellEvent

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
class RoundRectangle implements PdfPCellEvent {
  public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
    PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
    cb.setColorStroke(new GrayColor(0.8f));
    cb.roundRectangle(rect.left() + 4, rect.bottom(), rect.width() - 8, rect.height() - 4, 4);
    cb.stroke();
  }
}
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    RoundRectangle border = new RoundRectangle();
    PdfPTable table = new PdfPTable(6);
    PdfPCell cell;
    for (int i = 1; i <= 30; i++) {
      cell = new PdfPCell(new Phrase("day " + i));
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      cell.setBorder(Rectangle.NO_BORDER);
      cell.setPadding(4);
      cell.setCellEvent(border);
      table.addCell(cell);
    }
    document.add(table);
    document.close();
  }
}





Not scaled Image

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image img = Image.getInstance("dog.jpg");
    PdfPTable table = new PdfPTable(1);
    table.addCell(new PdfPCell(img, false));
    document.add(table);
    document.close();
  }
}





Rotate a table cell

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      float[] widths = { 1, 4 };
      PdfPTable table = new PdfPTable(widths);
      table.setWidthPercentage(30);
      PdfPCell cell = new PdfPCell(new Paragraph("fox"));
      cell.setRotation(90);
      table.addCell(cell);
      table.addCell("asdf");
      document.add(table);
    document.close();
  }
}





Scaled Image with no padding

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image img = Image.getInstance("dog.jpg");
    PdfPTable table = new PdfPTable(1);
    table.addCell(new PdfPCell(img, true));
    document.add(table);
    document.close();
  }
}





Set alignment for Table cell

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;
    Paragraph p = new Paragraph(".");
    table.addCell("default alignment");
    cell = new PdfPCell(p);
    table.addCell(cell);
    table.addCell("centered alignment");
    cell = new PdfPCell(p);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);
    table.addCell("right alignment");
    cell = new PdfPCell(p);
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(cell);
    table.addCell("justified alignment");
    cell = new PdfPCell(p);
    cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Set leading for table cell

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell(new Paragraph(
        "Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog."));
    table.addCell("default leading / spacing");
    table.addCell(cell);
    table.addCell("absolute leading: 20");
    cell.setLeading(20f, 0f);
    table.addCell(cell);
    table.addCell("absolute leading: 3; relative leading: 1.2");
    cell.setLeading(3f, 1.2f);
    table.addCell(cell);
    table.addCell("absolute leading: 0; relative leading: 1.2");
    cell.setLeading(0f, 1.2f);
    table.addCell(cell);
    table.addCell("no leading at all");
    cell.setLeading(0f, 0f);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Set left, right, top and bottom padding for table cell

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell(new Paragraph(
        "Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog."));
    table.addCell("different padding for left, right, top and bottom");
    cell.setPaddingLeft(20);
    cell.setPaddingRight(50);
    cell.setPaddingTop(0);
    cell.setPaddingBottom(5);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Set padding for table cell

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell(new Paragraph(
        "Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog."));
    cell = new PdfPCell(new Paragraph("Quick brown fox jumps over the lazy dog."));
    table.addCell("padding 10");
    cell.setPadding(10);
    table.addCell(cell);
    table.addCell("padding 0");
    cell.setPadding(0);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Set table cell fixed height

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A5.rotate());
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      table.setExtendLastRow(true);
      PdfPCell cell;
      cell = new PdfPCell(new Paragraph("blah"));
      table.addCell("wrap");
      cell.setNoWrap(false);
      table.addCell(cell);
      table.addCell("no wrap");
      cell.setNoWrap(true);
      table.addCell(cell);
      table.addCell(cell);
      table.addCell("fixed height (not sufficient)");
      cell.setFixedHeight(36f);
      table.addCell(cell);
      document.add(table);
    document.close();
  }
}





Set table cell minimun height

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A5.rotate());
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(2);
      table.setExtendLastRow(true);
      PdfPCell cell;
      cell = new PdfPCell(new Paragraph("blah"));
      table.addCell("wrap");
      cell.setNoWrap(false);
      table.addCell(cell);
      table.addCell("no wrap");
      cell.setNoWrap(true);
      table.addCell(cell);
      table.addCell(cell);
      table.addCell("fixed height (not sufficient)");
      cell.setMinimumHeight(36f);
      table.addCell(cell);
      document.add(table);
    document.close();
  }
}





Set table cell no wrap

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A5.rotate());
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setExtendLastRow(true);
    PdfPCell cell;
    cell = new PdfPCell(new Paragraph("blah"));
    table.addCell("wrap");
    cell.setNoWrap(false);
    table.addCell(cell);
    table.addCell("no wrap");
    cell.setNoWrap(true);
    table.addCell(cell);
    document.add(table);
    document.close();
  }
}





Set table column width

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      float[] widths = { 1, 4 };
      PdfPTable table = new PdfPTable(widths);
      table.setWidthPercentage(30);
      PdfPCell cell = new PdfPCell(new Paragraph("fox"));
      cell.setHorizontalAlignment(Element.ALIGN_CENTER);
      table.addCell(cell);
      table.addCell("asdf");
      document.add(table);
    document.close();
  }
}





Set width percentage

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      float[] widths = { 1, 4 };
      PdfPTable table = new PdfPTable(widths);
      table.setWidthPercentage(30);
      PdfPCell cell = new PdfPCell(new Paragraph("fox"));
      cell.setPadding(4);
      table.addCell(cell);
      table.addCell("asdf");
      document.add(table);
    document.close();
  }
}





Table alignment: bottom, middle and top

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.addCell("blah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\n");
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
    table.addCell("bottom");
    table.addCell("blah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\n");
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell("middle");
    table.addCell("blah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\nblah\n");
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);
    table.addCell("top");
    document.add(table);
    document.close();
  }
}





Table cell ascender and descender

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell(new Paragraph(
        "Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog."));
    Phrase p = new Phrase("Quick brown fox jumps over the lazy dog");
    table.getDefaultCell().setPadding(2);
    table.getDefaultCell().setUseAscender(false);
    table.getDefaultCell().setUseDescender(false);
    table.addCell("padding 2; no ascender, no descender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(true);
    table.getDefaultCell().setUseDescender(false);
    table.addCell("padding 2; ascender, no descender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(false);
    table.getDefaultCell().setUseDescender(true);
    table.addCell("padding 2; descender, no ascender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(true);
    table.getDefaultCell().setUseDescender(true);
    table.addCell("padding 2; ascender and descender");
    cell.setPadding(2);
    cell.setUseAscender(true);
    cell.setUseDescender(true);
    table.addCell(p);
    document.add(table);
    document.close();
  }
}





Table cell without border

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
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;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(4);
    table.setWidthPercentage(100);
    PdfPCell cell;
    cell = new PdfPCell(new Paragraph("t"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("d"));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setBackgroundColor(Color.red);
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("r"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("b"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("G:"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.25"));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setGrayFill(0.25f);
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.5"));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setGrayFill(0.5f);
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("0.75"));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setGrayFill(0.75f);
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph(":"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("a"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("b"));
    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("o"));
    cell.setBorderWidth(6f);
    cell.setBorderColor(Color.orange);
    table.addCell(cell);    
    document.add(table);
    document.close();
  }
}