I have an example code:
public static void createSamplePDF(String header[], String body[][]) throws Exception{ Document documento = new Document(); //Create new File File file = new File("D:/newFileName.pdf"); file.createNewFile(); FileOutputStream fop = new FileOutputStream(file); PdfWriter.getInstance(documento, fop); documento.open(); //Fonts Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); Font fontBody = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL); //Table for header PdfPTable cabetabla = new PdfPTable(header.length); for (int j = 0; j < header.length; j++) { Phrase frase = new Phrase(header[j], fontHeader); PdfPCell cell = new PdfPCell(frase); cell.setBackgroundColor(new BaseColor(Color.lightGray.getRGB())); cabetabla.addCell(cell); } documento.add(cabetabla); //Tabla for body PdfPTable tabla = new PdfPTable(header.length); for (int i = 0; i < body.length; i++) { for (int j = 0; j < body[i].length; j++) { tabla.addCell(new Phrase(body[i][j], fontBody)); } } documento.add(tabla); documento.close(); fop.flush(); fop.close(); }
just call:
createSamplePDF(new String[]{"col1", "col2"}, new String[][]{{"rw11", "rw12"},{"rw21", "rw22"}});
3d5oN source share