/** * This example was written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/26628678/manipulating-witdh-and-height-of-rows-and-cells-in-itextsharp */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.GrayColor; 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 ColumnWidthExample { public static final String DEST = "results/tables/column_width_example.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new ColumnWidthExample().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); float[] columnWidths = {1, 5, 5}; PdfPTable table = new PdfPTable(columnWidths); table.setWidthPercentage(100); table.getDefaultCell().setUseAscender(true); table.getDefaultCell().setUseDescender(true); Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE); PdfPCell cell = new PdfPCell(new Phrase("This is a header", f)); cell.setBackgroundColor(GrayColor.GRAYBLACK); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(3); table.addCell(cell); table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f)); for (int i = 0; i < 2; i++) { table.addCell("#"); table.addCell("Key"); table.addCell("Value"); } table.setHeaderRows(3); table.setFooterRows(1); table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (int counter = 1; counter < 101; counter++) { table.addCell(String.valueOf(counter)); table.addCell("key " + counter); table.addCell("value " + counter); } document.add(table); document.close(); } }