I'm making a PDF impression thanks to the iText class. I have the query made which shows me the data of the clients in my database, the query works both in SQL and in commands. I print the data in a PDF correctly but now I want to add that data in a table where we will see the names, surnames, ID, phone number, address, city, email of each client.
I make the following code to insert the table but when I open the PDF document (it is correctly generated by NetBeans) it says: "Error when opening the document."
Code: public class PDF_Clientes_Tabla { // Path of the file within the Netbeans project. public static String file = System.getProperty ("user.dir") + "/ lista_clientes_tabla.pdf";
public static void crearPDF(ArrayList<Cliente> clientes) throws DocumentException{
//Declaramos un documento como un objecto Document.
Document documento = new Document(PageSize.LETTER, 80, 80, 75, 75);
//writer es declarado como el método utilizado para escribir en el archivo.
PdfWriter writer = null;
try{
//Obtenemos la instancia del archivo a utilizar.
writer = PdfWriter.getInstance(documento, new FileOutputStream(archivo));
}catch(FileNotFoundException | DocumentException ex){
ex.getMessage();
}
//Agregamos un título al documento.
documento.addTitle("ARCHIVO PDF GENERADO DESDE JAVA");
//Abrimos el documento a editar.
documento.open();
try{
//Obtenemos la instancia de la imagen/logo.
Image imagen = Image.getInstance("..\imagenes\LOGO.png");
//Alineamos la imagen al centro del documento.
imagen.setAlignment(Image.ALIGN_CENTER);
//Agregamos la imagen al documento.
documento.add(imagen);
}catch(IOException | DocumentException ex){
ex.getMessage();
}
//Creamos un párrafo nuevo llamado "vacio1" para espaciar los elementos.
Paragraph vacio1 = new Paragraph();
vacio1.add("\n\n");
documento.add(vacio1);
//Declaramos un texto llamado "titulo" como Paragraph.
//Le podemos dar formato alineado, tamaño, color, etc.
Paragraph titulo = new Paragraph();
titulo.setAlignment(Paragraph.ALIGN_CENTER);
titulo.setFont(FontFactory.getFont("Times New Roman", 24, Font.BOLD, BaseColor.RED));
titulo.add("***LISTADO DE CLIENTES***");
try{
//Agregamos el texto "titulo" al documento.
documento.add(titulo);
}catch(DocumentException ex){
ex.getMessage();
}
//Creamos un párrafo nuevo llamado "saltolinea1" simulando un salto de linea para espaciar
//los elementos del PDF.
Paragraph saltolinea1 = new Paragraph();
saltolinea1.add("\n\n");
documento.add(saltolinea1);
//Variable con el numero de clientes que hay en la tabla clientes.
int numero_clientes = ConexionesPDFTabla.numero_clientes();
//Añadimos una tabla de 7 columnas.
PdfPTable tabla = new PdfPTable(7);
//Añadimos los títulos a la tabla.
tabla.addCell("NOMBRE");
tabla.addCell("APELLIDOS");
tabla.addCell("DNI");
tabla.addCell("TLF_CONTACTO");
tabla.addCell("DIRECCION");
tabla.addCell("CIUDAD");
tabla.addCell("EMAIL");
//Recorremos cada arrayList e imprimimos los resultados.
for (int i = 0; i < numero_clientes; i++){
tabla.addCell("NOMBRE");
tabla.addCell("APELLIDOS");
tabla.addCell("DNI");
tabla.addCell("TLF_CONTACTO");
tabla.addCell("DIRECCION");
tabla.addCell("CIUDAD");
tabla.addCell("EMAIL");
}
//Añadimos la tabla "tabla" al documento "documento".
documento.add(tabla);
//Cerramos el documento.
documento.close();
//Cerramos el writer.
writer.close();
}
}
The data is "confusing" but it is to try a simple table but it still does not work when I open the file.