Generate PDF with iText - Insert data in JAVA table

0

I would like to print in a PDF the information of all my clients with their names and surnames (two columns), I extracted these data from an SQL query. I print these data well in a PDF but I show them line by line what I want is to create a table within the PDF with two columns : one column called "name" (insert the name) and another column called "surnames" (insert surnames). I never made a table in iText.

Correct iText code that generates a PDF with customer data line by line, without tables:

package bbdd;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Generamos un PDF de un listado de clientes de la tabla clientes.
 * @author Osmar Maza Lastra
 */
public class PDF_Clientes {
    //Ruta del archivo dentro del proyecto de Netbeans.
    public static String archivo = System.getProperty("user.dir")+"/lista_clientes.pdf";


    public static void crearPDF(ArrayList<String> 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 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("***LISTA DE CLIENTES***");

        try{
            //Agregamos el texto al documento.
            documento.add(titulo);
        }catch(DocumentException ex){
            ex.getMessage();
        }

        //Creamos un párrafo nuevo llamado "saltolinea" simulando un salto de linea para espaciar
        //los elementos del PDF.
        Paragraph saltolinea = new Paragraph();
        saltolinea.add("\n\n");
        documento.add(saltolinea);

        //Creamos un párrafo llamado "parrafo" donde irá el contenido del PDF.
        Paragraph parrafo = new Paragraph();
        for (int i=0; i<clientes.size(); i++){
            parrafo = new Paragraph();
            parrafo.setAlignment(Paragraph.ALIGN_CENTER);
            parrafo.setFont(FontFactory.getFont("Times New Roman", 12, Font.BOLD, BaseColor.BLACK));
            //Añadimos al párrafo "parrafo" los clientes de la lista clientes.
            parrafo.add(clientes.get(i));
            //Añadimos ese párrafo "parrafo" al documento "documento".
            documento.add(parrafo);
        }

        //Cerramos el documento.
        documento.close();
        //Cerramos el writer.
        writer.close();
    }
}
    
asked by omaza1990 15.12.2016 в 09:25
source

1 answer

1

The addCell() function adds a cell to the table, the row change occurs automatically when filling the row.

table.addCell("Celda 1");
table.addCell("Celda 2");
table.addCell("Celda 3");

If you want to create a cell with more than one column, create a Cell object and change its span property.

PdfPCell celda = new PdfPCell(new Paragraph("Fin tabla"));

Indicates how many columns the cell occupies

celda.setColspan(3);
table.addCell(celda);

Add the table to the document

document.add(table);
    
answered by 14.06.2017 в 11:19