1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import com.itextpdf.text.Document; 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 TableExample { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("HelloWorld-Table.pdf")); document.open(); PdfPTable table = new PdfPTable(3); // 3 columns. PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2")); PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3")); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); document.add(table); document.close(); } catch (Exception e) { } } } |
Table Width
1 |
table.setWidthPercentage(100); |
Spacing Before and After Table
1 2 |
table.setSpacingBefore(10f); table.setSpacingAfter(10f); |
Column Span
1 |
cell.setColspan(2); |
Cell Alignment
1 2 |
cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); |
Cell Padding
1 2 3 4 5 6 |
cell.setPadding(5); cell.setPaddingLeft(8); cell.setPaddingRight(8); cell.setPaddingTop(8); cell.setPaddingBottom(8); |