Add table cell to table : Table « PDF « Java Tutorial






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.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
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(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfPTable datatable = new PdfPTable(10);
    int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
    datatable.setWidths(headerwidths);
    datatable.setWidthPercentage(100);
    datatable.getDefaultCell().setPadding(5);

    datatable.setHeaderRows(2);

    datatable.getDefaultCell().setBorderWidth(1);
    for (int i = 1; i < 30; i++) {
      datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
      datatable.addCell("myUserId");
      datatable.addCell("long long name");
      datatable.addCell("No Name Company");
      datatable.addCell("D" + i);
      datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
      for (int j = 0; j < 6; j++)
        datatable.addCell(Math.random() > .5 ? "Yes" : "No");
    }
    datatable.setSplitLate(false);
    document.add(datatable);

    document.close();
  }
}








29.54.Table
29.54.1.Pdf Table Split Vertically
29.54.2.Generates a PDF file with a table
29.54.3.Pdf Table Without Borders
29.54.4.Pdf Table with Absolute Width
29.54.5.Nested Pdf Table
29.54.6.Table Aligned
29.54.7.Skip header
29.54.8.Set Spacing Before/After a table
29.54.9.Use Table to create Report header
29.54.10.We need 4 cells with rowspan 2
29.54.11.Add table cell to table