/** * Example written for JavaOne 2016. * Differences between iText 5 and iText 7 are discussed in the JavaOne talk * "Oops, I broke my API" by Raf Hens and Bruno Lowagie. * This is the iText 5 version of one of the examples. */ package javaone.edition16; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class HelloWorldTable { public static final String DEST = "results/javaone/edition16/hello_table.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new HelloWorldTable().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(3); PdfPCell cell = new PdfPCell(new Phrase("Cell with colspan 3")); cell.setColspan(3); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell with rowspan 2")); cell.setRowspan(2); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); table.addCell("Cell 1.1"); cell = new PdfPCell(); cell.addElement(new Phrase("Cell 1.2")); table.addCell(cell); cell = new PdfPCell(new Phrase("Cell 2.1")); cell.setPadding(5); cell.setUseAscender(true); cell.setUseDescender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(); cell.setPadding(5); cell.setUseAscender(true); cell.setUseDescender(true); Paragraph p = new Paragraph("Cell 2.2"); p.setAlignment(Element.ALIGN_CENTER); cell.addElement(p); table.addCell(cell); document.add(table); document.close(); } }