Error opening the file. Fault PdfPTable iText - JAVA

0

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.

    
asked by omaza1990 26.12.2016 в 12:44
source

1 answer

1

You're adding to a 7-column table, the entire client toString, you should do something like this:

int numero_clientes = ConexionesPDFTabla.numero_clientes();
PdfPTable tabla = new PdfPTable(7);
// Aqui puedes añadir una fila con los titulos
//tabla.addCell("Nombre");
//tabla.addCell("DNI");
//Y asi los 7 campos
for (int i = 0; i < numero_clientes; i++){

    tabla.addCell(clientes.get(i).getNombre());
    tabla.addCell(clientes.get(i).getApellidos());
    //Y asi los 7 campos
}
documento.add(tabla);

If you want the titles in bold, I'll give you one example.

Paragraph cabecera=new Paragraph("Nombre");
cabecera.getFont().setStyle(Font.BOLD);
Cell cell=new Cell(cabecera);
table.addCell(cell);

EDIT as you receive customers:

for (int i = 0; i < numero_clientes; i++){
        String[] cliente=clientes.get(i).split(" ");
        tabla.addCell(cliente[0]);
        tabla.addCell(cliente[1]);
        //Y asi los 7 campos
    }
    
answered by 27.12.2016 / 09:36
source