El objeto para crear una tabla es PdfPTable, al cual podemos especificarle la cantidad de columnas que queremos. para rellenar la tabla, podemos utilizar:
mitabla.addCell("Contenido");
o para mayor control, podemos utilizarla de esta forma:
PdfPCell celda =new PdfPCell (new Paragraph("Historial de Observaciones",
FontFactory.getFont("arial", // fuente
22, // tamaño
Font.BOLD, // estilo
BaseColor.RED))); // color
mitabla.addCell(celda);
Itext va rellenando la tabla, celda por celda, de izquierda a derecha y de abajo a arriba, de forma secuencial. Aqui puedes encontrar mayor informacion de la API de Itext.
El Codigo:
Esta clase que se llama tablaspdf.java, contiene dos metodos, para crear archivos pdf con tablas, simples y complejas.
01 import com.itextpdf.text.BaseColor; 02 import com.itextpdf.text.Document; 03 import com.itextpdf.text.DocumentException; 04 import com.itextpdf.text.Element; 05 import com.itextpdf.text.Font; 06 import com.itextpdf.text.FontFactory; 07 import com.itextpdf.text.Paragraph; 08 import com.itextpdf.text.pdf.PdfPCell; 09 import com.itextpdf.text.pdf.PdfPTable; 10 import com.itextpdf.text.pdf.PdfWriter; 11 import java.io.File; 12 import java.io.FileNotFoundException; 13 import java.io.FileOutputStream; 14 /** 15 * @web http://jc-mouse.blogspot.com/ 16 * @author Mouse 17 */ 18 public class tablaspdf { 19 20 public void tablaspdf() throws DocumentException, FileNotFoundException{ 21 // se crea instancia del documento 22 Document mipdf = new Document(); 23 // se establece una instancia a un documento pdf 24 PdfWriter.getInstance(mipdf, new FileOutputStream(new File("e:/pdf/tablas.pdf"))); 25 mipdf.open(); // se abre el documento 26 27 mipdf.addTitle("Tablas con itext"); // se añade el titulo 28 mipdf.addAuthor("jc mouse"); // se añade el autor del documento 29 mipdf.addSubject("ocio"); //se añade el asunto del documento 30 mipdf.addKeywords("jc mouse"); //Se agregan palabras claves 31 32 /* ahora si tablas */ 33 //mipdf.add(Tabla_Simple()); 34 mipdf.add(Tabla_compleja()); 35 mipdf.close(); //se cierra el PDF 36 System.out.println("documento pdf creado"); 37 } 38 39 public PdfPTable Tabla_Simple(){ 40 //creamos la tabla con 3 columnas 41 PdfPTable mitablasimple=new PdfPTable(3); 42 //añadimos contenido a las celdas 43 mitablasimple.addCell("Enero"); 44 mitablasimple.addCell("Febrero"); 45 mitablasimple.addCell("Marzo"); 46 mitablasimple.addCell("Abril"); 47 mitablasimple.addCell("Mayo"); 48 mitablasimple.addCell("Junio"); 49 mitablasimple.addCell("Julio"); 50 mitablasimple.addCell("Agosto"); 51 mitablasimple.addCell("Septiembre"); 52 mitablasimple.addCell("Octubre"); 53 //retornamos la tabla 54 return mitablasimple; 55 } 56 57 public PdfPTable Tabla_compleja(){ 58 //creamos una tabla con 3 columnas 59 PdfPTable mitablacompleja=new PdfPTable(3); 60 //añadimos texto con formato a la primera celda 61 PdfPCell celda =new PdfPCell (new Paragraph("Historial de Observaciones", 62 FontFactory.getFont("arial", // fuente 63 22, // tamaño 64 Font.BOLD, // estilo 65 BaseColor.RED))); // color 66 //unimos esta celda con otras 2 67 celda.setColspan(3); 68 //alineamos el contenido al centro 69 celda.setHorizontalAlignment(Element.ALIGN_CENTER); 70 // añadimos un espaciado 71 celda.setPadding (12.0f); 72 //colocamos un color de fondo 73 celda.setBackgroundColor(BaseColor.GRAY); 74 //se añade a la tabla 75 mitablacompleja.addCell(celda); 76 77 //fila 2 78 celda = new PdfPCell(new Paragraph ("AUDITORIA DE SISTEMAS")); 79 celda.setColspan(2); 80 celda.setBackgroundColor(BaseColor.GREEN); 81 mitablacompleja.addCell(celda); 82 celda = new PdfPCell(new Paragraph ("Aprobado")); 83 celda.setBackgroundColor(BaseColor.LIGHT_GRAY); 84 mitablacompleja.addCell(celda); 85 //fila 3 86 celda = new PdfPCell(new Paragraph ("COMPILADORES")); 87 celda.setColspan(2); 88 celda.setBackgroundColor(BaseColor.YELLOW); 89 mitablacompleja.addCell(celda); 90 celda = new PdfPCell(new Paragraph ("Reprobado")); 91 celda.setBackgroundColor(BaseColor.LIGHT_GRAY); 92 mitablacompleja.addCell(celda); 93 //fila 4 94 celda = new PdfPCell(new Paragraph ("Prog. Bajo Nivel")); 95 celda.setColspan(2); 96 celda.setBackgroundColor(BaseColor.CYAN); 97 mitablacompleja.addCell(celda); 98 celda = new PdfPCell(new Paragraph ("Eximido")); 99 celda.setBackgroundColor(BaseColor.LIGHT_GRAY); 100 mitablacompleja.addCell(celda); 101 //fila 5 102 mitablacompleja.addCell("Conclusion"); 103 celda = new PdfPCell(new Paragraph ("¡¡¡GET A LIFE!!!")); 104 celda.setHorizontalAlignment(Element.ALIGN_RIGHT); 105 celda.setColspan(2); 106 celda.setBackgroundColor(BaseColor.ORANGE); 107 mitablacompleja.addCell(celda); 108 // se retorna la tabla 109 return mitablacompleja; 110 } 111 }
El resultado es:















