Example usage for com.itextpdf.text.pdf PdfPTable PdfPTable

List of usage examples for com.itextpdf.text.pdf PdfPTable PdfPTable

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPTable PdfPTable.

Prototype

public PdfPTable(final PdfPTable table) 

Source Link

Document

Constructs a copy of a PdfPTable.

Usage

From source file:comisionesafis.informes.LiquidacionComisiones.java

private PdfPTable creaTabla() {

    float[] anchuras = { 2f, 1f, 1f, 1f, 1f };
    PdfPTable tabla = new PdfPTable(anchuras);
    tabla.setWidthPercentage(100);//from  w  w w .  j av  a2 s . c om
    tabla.setSpacingBefore(4f);
    return tabla;

}

From source file:control.AdminPDF.java

public static void generarInforme(ArrayList<Tarea> tareas, String usuario) {

    try {/*from w  w  w  .  j  a  va  2s  .c  o  m*/
        //Creamos la carpeta donde se almacenaran las facturas
        // Si quieres que las facturas se guarden en una carpeta del computador, 
        // aca colocas la ruta donde quieres que se guarden en lugar de src/facturas, 
        // tanto al crear carpeta y laFactura (de tipo File) y al crear archivo (de tipo FileOutputStream)
        //Ejemplo:  File carpeta = new File("D:\\facturas"); 
        //          FileOutputStream archivo = new FileOutputStream("D:\\facturas" + consecutivo + ".pdf");
        //          File laFactura = new File("src/facturas" + consecutivo + ".pdf");

        File carpeta = new File("web/pdfs");
        System.out.println(carpeta.exists());
        if (!carpeta.exists()) {
            carpeta.mkdirs();
        }
        String[] list = carpeta.list();
        consecutivo = list.length;
        System.out.println("Consecutivo es: " + consecutivo);
        int total = 0;
        FileOutputStream archivo = new FileOutputStream("web/pdfs/" + consecutivo + ".pdf");
        Document documento = new Document();
        PdfWriter.getInstance(documento, archivo);
        documento.open();
        Paragraph titulo = new Paragraph("Resumen de Actividades");
        titulo.setAlignment(Element.ALIGN_CENTER);
        documento.add(titulo);

        documento.add(new Paragraph(
                "------------------------------------------------------------------------------------------------------------------------------"));
        documento.add(new Paragraph("Usuario: " + usuario));
        documento.add(new Paragraph(" "));
        //         documento.add(new Paragraph("------------------------------------------------------------------------------------------------------------------------------"));
        documento.add(new Paragraph(" "));
        documento.add(new Paragraph(" "));
        //         documento.add(new Paragraph(" "));
        //         documento.add(new Paragraph(" "));
        documento.add(new Paragraph("  "));
        PdfPTable tabla = new PdfPTable(7);
        Image image = Image.getInstance("../EasyTasks/web/images/logo.png");

        //         tabla.addCell(image);
        Paragraph nombreBase = new Paragraph("Nombre ");
        Paragraph descripcionBase = new Paragraph("Descripcion     ");
        Paragraph fechaBase = new Paragraph("Fecha ");
        Paragraph prioridadBase = new Paragraph("Prioridad  ");
        Paragraph tagsBase = new Paragraph("Tags");
        Paragraph categoriaBase = new Paragraph("Categoria  ");
        Paragraph ubicacionBase = new Paragraph("Ubicacion    ");

        //         tabla.addCell(image);
        tabla.addCell(nombreBase);
        tabla.addCell(descripcionBase);
        tabla.addCell(fechaBase);
        tabla.addCell(prioridadBase);
        tabla.addCell(tagsBase);
        tabla.addCell(categoriaBase);
        tabla.addCell(ubicacionBase);
        for (int i = 0; i < tareas.size(); i++) {

            Tarea tareaNueva = tareas.get(i);
            Paragraph nombre = new Paragraph(tareaNueva.getNombre());
            Paragraph descripcion = new Paragraph(tareaNueva.getDescripcion());
            Paragraph fecha = new Paragraph(tareaNueva.getFechaLimite() + "");
            Paragraph prioridad = new Paragraph(tareaNueva.getPrioridad());
            Paragraph tags = null;
            for (int k = 0; k < tareaNueva.getTags().size(); k++) {
                tags = new Paragraph(tareaNueva.getTags().get(k));
            }
            Paragraph categoria = new Paragraph(tareaNueva.getCategoria());
            Paragraph ubicacion = new Paragraph(
                    tareaNueva.getUbiLatitud() + ", " + tareaNueva.getUbiLongitud());
            //            Paragraph tost = new Paragraph(tareaNueva.toStringSimple());

            tabla.addCell(nombre);
            tabla.addCell(descripcion);
            tabla.addCell(fecha);
            tabla.addCell(prioridad);
            tabla.addCell(tags);
            tabla.addCell(categoria);
            tabla.addCell(ubicacion);
            //            tabla.addCell(tost);

        }

        //         FileReader fr = new FileReader("src/archivos/pedidos.txt");
        //         BufferedReader bwa = new BufferedReader(fr);
        //         String elProducto = bwa.readLine();
        //         while (elProducto != null) {
        //            if (!elProducto.equalsIgnoreCase("")) {
        //               System.out.println("El producto: " + elProducto);
        //               String[] elProductoConPrecio = elProducto.split("-");
        //               Paragraph producto = new Paragraph(elProductoConPrecio[0]);
        //               Paragraph precio = new Paragraph(elProductoConPrecio[1]);
        //               total += Integer.parseInt(elProductoConPrecio[1]);
        //               precio.setAlignment(Element.ALIGN_RIGHT);
        //               tabla.addCell(producto);
        //               tabla.addCell(precio);
        //               elProducto = bwa.readLine();
        //            }
        //         }
        //         if (!darIva().equals("") && !darIva().equals("0")) {
        //            tabla.addCell("Subtotal");
        //            Paragraph elSubTotal = new Paragraph("" + total);
        //            elSubTotal.setAlignment(Element.ALIGN_RIGHT);
        //            tabla.addCell(elSubTotal);
        //            tabla.addCell("Iva (" + darIva() + ")");
        //            int iva = total * Integer.parseInt(darIva()) / 100;
        //            tabla.addCell("" + iva);
        //            int precioConIva = total + iva;
        //            tabla.addCell("Total");
        //            Paragraph elTotal = new Paragraph("" + precioConIva);
        //            elTotal.setAlignment(Element.ALIGN_RIGHT);
        //            tabla.addCell(elTotal);
        //         } else {
        //            tabla.addCell("Total");
        //            Paragraph elTotal = new Paragraph("" + total);
        //            elTotal.setAlignment(Element.ALIGN_RIGHT);
        //            tabla.addCell(elTotal);
        //         }
        documento.add(tabla);
        documento.add(new Paragraph(
                "----------------------------------------------------------------------------------------------"));
        documento.add(new Paragraph("Muchas gracias por utilizar nuestro servicio,"));
        documento.add(new Paragraph(""));
        //         Image image = Image.getInstance("logo.png");
        documento.addCreator("EasyTask Team");
        documento.add(image);
        documento.addCreationDate();
        //         PdfPCell cell2 = new PdfPCell(image, true);

        //         tabla.addCell(cell2);
        documento.add(new Paragraph(""));
        documento.add(new Paragraph("         EasyTask Team"));
        documento.close();
        File elInforme = new File("../EasyTasks/web/pdfs/" + usuario + ".pdf");
        Desktop.getDesktop().open(elInforme);
        //         BufferedWriter br = new BufferedWriter(new FileWriter("src//pedidos.txt"));
        //         br.write("");
        //         br.close();
        //         fr.close();
        //         bwa.close();
        archivo.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(AdminPDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(AdminPDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(AdminPDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
    }

}

From source file:control.listaEquipos.java

private boolean generapdf(String path) throws DocumentException, FileNotFoundException {
    boolean estado = false;
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//from  w  w  w.java 2 s . c o  m
    Paragraph paragraph1 = new Paragraph("Equipos");
    document.add(paragraph1);
    Paragraph paragraph2 = new Paragraph("");
    paragraph2.setSpacingBefore(38f);
    document.add(paragraph2);
    PdfPTable table = new PdfPTable(7);
    table.addCell("Id ");
    table.addCell("Nombre");
    table.addCell("categoria");
    table.addCell("Accesorios");
    table.addCell("Descripcion");
    table.addCell("Marca");
    table.addCell("Estado");
    Object[][] tabla = this.getTableData(tabEquipo);
    for (Object[] o : tabla) {
        int i = 0;
        for (Object e : o) {
            if (i <= 6) {
                if (e != null)
                    table.addCell(((String) e.toString()));
                else
                    table.addCell("");
            }
            i++;
        }
    }
    document.add(table);
    document.close();
    return true;
}

From source file:control.listaEquiposUsua.java

private boolean generapdf(String path) throws DocumentException, FileNotFoundException {
    boolean estado = false;
    Document document = new Document();
    //OutputStreamWriter destino = new OutputStreamWriter(fos,"ISO-8859-1");

    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();/* w  ww  .ja  va2s.  c o  m*/
    Paragraph paragraph1 = new Paragraph("Equipos");
    document.add(paragraph1);
    Paragraph paragraph2 = new Paragraph("");
    paragraph2.setSpacingBefore(38f);
    document.add(paragraph2);
    PdfPTable table = new PdfPTable(7);
    table.addCell("Id ");
    table.addCell("Nombre");
    table.addCell("categoria");
    table.addCell("Accesorios");
    table.addCell("Descripcion");
    table.addCell("Marca");
    table.addCell("Estado");
    Object[][] tabla = this.getTableData(tabEquipo);
    for (Object[] o : tabla) {
        int i = 0;
        for (Object e : o) {
            if (i <= 6) {
                if (e != null)
                    table.addCell((String) e.toString());
                else
                    table.addCell("");
            }
            i++;
        }
    }
    document.add(table);
    document.close();
    return true;
}

From source file:control.listaReporte.java

private boolean generapdf(String path) throws DocumentException, FileNotFoundException {
    boolean estado = false;
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//from   w w  w.j  ava  2 s  .c o m
    Paragraph paragraph1 = new Paragraph("Prestamos");
    document.add(paragraph1);
    Paragraph paragraph2 = new Paragraph("");
    paragraph2.setSpacingBefore(38f);
    document.add(paragraph2);
    PdfPTable table = new PdfPTable(7);
    table.addCell("Id reporte");
    table.addCell("Solicitante");
    table.addCell("Proyecto");
    table.addCell("Fecha prestamo");
    table.addCell("Fecha devolucion");
    table.addCell("Fecha real");
    table.addCell("prestador");
    Object[][] tabla = this.getTableData(reportes);
    for (Object[] o : tabla)
        for (Object e : o) {
            if (e != null)
                table.addCell((String) e.toString());
            else
                table.addCell("");
        }

    document.add(table);
    document.close();
    return true;
}

From source file:control.ModificaPrestamo.java

public boolean generapdf(String path) throws DocumentException, FileNotFoundException {
    boolean estado = false;
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//  w w w. j  a  v a 2s. c  o m

    Image img = null;
    try {
        img = Image.getInstance("src/img/imgen.jpg");
        img.scaleAbsolute(80f, 50f);
        img.setAbsolutePosition(470f, 750f);
        document.add(img);
    } catch (BadElementException ex) {
        Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex);
    }

    Paragraph paragraph1 = new Paragraph("Solicitante: " + Solicitante.getText());
    Paragraph paragraph2 = new Paragraph("Proyecto: " + Proyecto.getText());
    Paragraph paragraph3 = new Paragraph(
            "Fecha y hora del prestamo: " + fecha.getText() + " " + hora.getText());
    Paragraph paragraph4 = new Paragraph("Fecha de entrega: " + fecha1.getText());

    document.add(paragraph1);
    document.add(paragraph2);
    document.add(paragraph3);
    document.add(paragraph4);
    Paragraph tmp = new Paragraph("");
    tmp.setSpacingBefore(10);
    document.add(tmp);
    PdfPTable table = new PdfPTable(4);
    table.addCell("Id Equipo");
    table.addCell("Equipo");
    table.addCell("Accesorios");
    table.addCell("Descripcion");
    Object[][] tabla = this.getTableData(tabReporte);
    for (Object[] o : tabla)
        for (Object e : o) {
            table.addCell((String) e);
        }

    document.add(table);
    System.out.println(clausula1.getText() + " " + clausula2.getText());
    Paragraph paragraph5 = new Paragraph("Clausula 1: " + clausula1.getText());
    Paragraph paragraph6 = new Paragraph("Clausula 2: " + clausula2.getText());
    document.add(paragraph5);
    document.add(paragraph6);
    tmp = new Paragraph("");
    tmp.setSpacingBefore(10);
    document.add(tmp);
    Paragraph p = new Paragraph("Firma del solicitante: ");
    p.add(new Chunk(new DottedLineSeparator()));
    p.add("   Firma del prestador: ");
    p.add(new Chunk(new DottedLineSeparator()));
    document.add(p);

    //PdfPTable firma = new PdfPTable(1);
    //firma.addCell("Firma");
    //document.add(new Paragraph(""));
    //document.add(firma);
    document.close();
    return true;
}

From source file:Control.ProcessaArq.java

public void gravaArquivoConsumoUnidades(ArrayList<Usuario> listaUsuario, String caminho)
        throws IOException, DocumentException {

    Document doc = null;/*from   w  w w  .  j  a v a2s . c  o  m*/
    OutputStream os = null;
    PdfWriter pdf = null;
    Paragraph p = new Paragraph();
    Tratamento tempo = new Tratamento();
    PdfPTable table = new PdfPTable(5);
    DecimalFormat df = new DecimalFormat("#####.##");
    ArrayList<String> listaCidade = new ArrayList<String>();
    String local = new String();
    String informacao, tipo = new String();
    Double soma, valorTotal, diferenca = 0.0;
    Usuario unidade = new Usuario();
    ArrayList<Usuario> listaUnidades = new ArrayList<Usuario>();
    /*    
        if(banco.getConta().get(banco.getConta().size()-1).getDiferenca() > 0){
    diferenca = banco.getConta().get(banco.getConta().size()-1).getDiferenca()/9;
        }
        else{ diferenca = 0.0; }
    */
    for (int a = 0; a <= listaUsuario.size() - 1; a++) {
        if ((!listaCidade.contains(listaUsuario.get(a).getCidade()))) {
            listaCidade.add(listaUsuario.get(a).getCidade());
        }
    }

    float[] headerwidths = { 30, 80, 35, 35, 25 }; // define a largura de cada coluna       
    table.setWidths(headerwidths);

    caminho = localizaArquivo(false) + ".pdf";
    try {//configuraes da pgina          
        float fntSize, lineSpacing;
        fntSize = 8f;
        lineSpacing = 8f;

        doc = new Document(PageSize.A4.rotate(), 10, 10, 30, 40);
        os = new FileOutputStream(caminho);
        pdf.getInstance(doc, os);
        doc.open();

        /*
        local = new String();
        local = caminho +" "+ listaCidade.get(a) + ".pdf";
                
        doc = new Document(PageSize.A4.rotate(), 10, 10, 30, 40); 
        os = new FileOutputStream(local);            
        pdf.getInstance(doc, os);   
        doc.open();
        */
        //cabecalho da tabela
        table.getDefaultCell().setBorder(0);

        table.getDefaultCell().setBorder(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        table.getDefaultCell().setColspan(5);

        table.addCell(new Paragraph("CONSUMO POR UNIDADE " + banco.periodoFatura()));

        table.getDefaultCell().setBorder(0);
        table.getDefaultCell().setColspan(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        doc.add(table);

        for (int a = 0; a <= listaCidade.size() - 1; a++) {

            table = new PdfPTable(5);
            table.setWidths(headerwidths);
            table.getDefaultCell().setBorder(0);
            table.addCell(new Paragraph(""));
            table.addCell(new Paragraph(""));
            table.addCell(new Paragraph(""));
            table.addCell(new Paragraph(""));
            table.addCell(new Paragraph(""));
            table.getDefaultCell().setBorder(1);

            table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
            table.addCell(new Paragraph("Cidade"));
            table.addCell(new Paragraph("Usurio"));
            table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            table.addCell(new Paragraph("Nmero"));
            table.addCell(new Paragraph("Tipo"));
            table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            table.addCell(new Paragraph("Valor"));
            table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

            soma = 0.0;
            for (int b = 0; b <= listaUsuario.size() - 1; b++) {

                if (listaCidade.get(a).equals(listaUsuario.get(b).getCidade())) {

                    //cidade
                    informacao = new String();
                    informacao = listaUsuario.get(b).getCidade();
                    table.addCell(new Paragraph(informacao));

                    //Usurio
                    informacao = new String();
                    informacao = listaUsuario.get(b).getNome();
                    table.addCell(new Paragraph(informacao));

                    //Nmero
                    informacao = new String();
                    informacao = String.valueOf(listaUsuario.get(b).getLinha());
                    table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    table.addCell(new Paragraph(informacao));

                    //Tipo
                    informacao = new String();
                    informacao = listaUsuario.get(b).getTipo();
                    table.addCell(new Paragraph(informacao));

                    //Valor
                    informacao = new String();
                    soma += listaUsuario.get(b).getValor();
                    informacao = "R$" + df.format(listaUsuario.get(b).getValor());
                    table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
                    table.addCell(new Paragraph(informacao));
                }
            }

            // Total
            table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
            informacao = new String();
            informacao = "Total apurado";
            table.addCell(informacao);

            // tempo                        
            table.addCell("");
            table.addCell("");
            table.addCell("");

            // valor
            informacao = new String();
            informacao = "R$" + df.format(soma);
            table.addCell(informacao);
            table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

            if ((soma - diferenca) > 0) {
                table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
                informacao = new String();
                informacao = "Total a pagar";
                table.addCell(informacao);

                // tempo                        
                table.addCell("");
                table.addCell("");
                table.addCell("");

                // valor
                informacao = new String();
                informacao = "R$" + df.format(soma - diferenca);
                table.addCell(informacao);
                table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            }

            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");

            unidade = new Usuario();
            unidade.setCidade(listaCidade.get(a));
            unidade.setValor(soma);
            listaUnidades.add(unidade);

            doc.add(table);
        }

        doc.newPage();
        table.getDefaultCell().setBorder(0);
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.getDefaultCell().setBorder(1);

        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(new Paragraph("Cidade"));
        table.addCell(new Paragraph(""));
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(new Paragraph("Valor"));
        table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

        soma = 0.0;
        for (int a = 0; a <= listaUnidades.size() - 1; a++) {
            //cidade
            informacao = new String();
            informacao = listaUnidades.get(a).getCidade();
            table.addCell(new Paragraph(informacao));

            table.addCell("");
            table.addCell("");
            table.addCell("");

            //Valor
            informacao = new String();
            soma += listaUnidades.get(a).getValor();
            informacao = "R$" + df.format(listaUnidades.get(a).getValor());
            table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            table.addCell(new Paragraph(informacao));
        }

        // Total
        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        informacao = new String();
        informacao = "Total apurado";
        table.addCell(informacao);

        // tempo                        
        table.addCell("");
        table.addCell("");
        table.addCell("");

        // valor
        informacao = new String();
        informacao = "R$" + df.format(soma);
        table.addCell(informacao);
        table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        informacao = new String();
        informacao = "Total a pagar";
        table.addCell(informacao);

        // tempo                        
        table.addCell("");
        table.addCell("");
        table.addCell("");

        // valor
        informacao = new String();
        informacao = "R$" + df.format(soma);
        //    informacao = "R$"+ df.format(soma-banco.getConta().get(banco.getConta().size()-1).getDiferenca());
        table.addCell(informacao);
        table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

        doc.add(table);
        doc.close();
        os.close();

    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}

From source file:Control.ProcessaArq.java

public void gravaArquivoConsumoUsuarios(ArrayList<Usuario> lista, String caminho)
        throws IOException, DocumentException {

    Document doc = null;/*from   www. j  a v  a 2  s.co m*/
    OutputStream os = null;
    Paragraph p = new Paragraph();
    Tratamento tempo = new Tratamento();
    DecimalFormat df = new DecimalFormat("#####.##");
    String informacao, tipo = new String();
    Double soma, valorTotal = 0.0;

    PdfPTable table = new PdfPTable(5); //tabela com 5 colunas
    float[] headerwidths = { 30, 80, 35, 35, 25 }; // define a largura de cada coluna  
    table.setWidths(headerwidths);

    caminho = localizaArquivo(false) + ".pdf";
    try {//configuraes da pgina          
        float fntSize, lineSpacing;
        fntSize = 8f;
        lineSpacing = 8f;
        doc = new Document(PageSize.A4.rotate(), 10, 10, 30, 40);

        os = new FileOutputStream(caminho);
        PdfWriter.getInstance(doc, os);
        doc.open();

        //cabecalho da tabela
        table.getDefaultCell().setBorder(0);
        table.getWidthPercentage();

        table.getDefaultCell().setBorder(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        table.getDefaultCell().setColspan(5);

        table.addCell(new Paragraph("CONSUMO POR USU?RIOS ") + banco.periodoFatura());

        table.getDefaultCell().setBorder(1);
        table.getDefaultCell().setColspan(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.addCell(new Paragraph(""));
        table.getDefaultCell().setBorder(1);

        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(new Paragraph("Cidade"));
        table.addCell(new Paragraph("Usurio"));
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        table.addCell(new Paragraph("Nmero"));
        table.addCell(new Paragraph("Tipo"));
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        table.addCell(new Paragraph("Valor"));
        table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

        soma = 0.0;
        for (int a = 0; a <= lista.size() - 1; a++) {

            //cidade
            informacao = new String();
            informacao = lista.get(a).getCidade();
            //    table.setTotalWidth(80);
            table.addCell(new Paragraph(informacao));

            //Usurio
            informacao = new String();
            informacao = lista.get(a).getNome();
            //    table.setTotalWidth(200);
            table.addCell(new Paragraph(informacao));

            //Nmero
            informacao = new String();
            informacao = String.valueOf(lista.get(a).getLinha());
            //    table.setTotalWidth(10);
            table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            table.addCell(new Paragraph(informacao));

            //Tipo
            informacao = new String();
            informacao = lista.get(a).getTipo();
            //    table.setTotalWidth(10);
            table.addCell(new Paragraph(informacao));

            //Valor
            informacao = new String();
            soma += lista.get(a).getValor();
            informacao = "R$" + df.format(lista.get(a).getValor());
            //    table.setTotalWidth(10);
            table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            table.addCell(new Paragraph(informacao));
        }

        // Total
        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        informacao = new String();
        informacao = "Total";
        table.addCell(informacao);

        // tempo                               
        table.addCell("");
        table.addCell("");
        table.addCell("");

        // valor
        informacao = new String();
        informacao = "R$" + df.format(soma);
        table.addCell(informacao);
        table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

        doc.add(table);
        // fim da pgina
    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}

From source file:Control.ProcessaArq.java

public void gravaArquivoServicos(ArrayList<Conta> lista, String caminho) throws IOException, DocumentException {
    ArrayList<String> listaTipos;
    String local[];/* ww w . j  a v  a 2s .c  o m*/
    Document doc = null;
    OutputStream os = null;
    Paragraph p = new Paragraph();
    Tratamento tempo = new Tratamento();
    DecimalFormat df = new DecimalFormat("#####.##");
    String informacao, tempoServico, tempoSubTotal, tempoTotal, tipo = new String();
    Double soma, valorTotal = 0.0;

    PdfPTable table = new PdfPTable(3); //tabela com 3 colunas
    float[] headerwidths = { 85, 35, 25 }; // define a largura de cada coluna
    table.setWidths(headerwidths);

    caminho = localizaArquivo(false) + ".pdf";
    try {//configuraes da pgina          
        float fntSize, lineSpacing;
        fntSize = 10f;
        lineSpacing = 10f;
        doc = new Document(PageSize.A4.rotate(), 10, 10, 20, 10);

        os = new FileOutputStream(caminho);
        PdfWriter.getInstance(doc, os);
        doc.open();

        tempoTotal = "00:00:00";
        tempoServico = "00:00:00";
        tempoSubTotal = "00:00:00";
        listaTipos = new ArrayList<String>();

        //cabecalho da tabela
        table.getDefaultCell().setBorder(0);
        table.getWidthPercentage();

        table.getDefaultCell().setBorder(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        table.getDefaultCell().setColspan(3);

        table.addCell(new Paragraph(tempo.verificaNomeServico(lista) + " " + banco.periodoFatura()));

        table.getDefaultCell().setBorder(1);
        table.getDefaultCell().setColspan(0);
        table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

        for (int a = 0; a < lista.size() - 1; a++) {
            System.out.println(a + " - " + lista.get(a).getTipo());

            if (!listaTipos.contains(lista.get(a).getTipo())) {

                listaTipos.add(lista.get(a).getTipo());
                soma = 0.0;
                tipo = new String();
                informacao = new String();

                informacao = "" + listaTipos.get(listaTipos.size() - 1) + "";
                table.getDefaultCell().setBorder(1);
                table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.getDefaultCell().setColspan(3);
                table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);

                table.addCell(new Paragraph(informacao));

                table.getDefaultCell().setBorder(1);
                table.getDefaultCell().setColspan(0);
                table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
                table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

                // contedo da tabela
                for (int b = 0; b <= lista.size() - 1; b++) {
                    if (lista.get(b).getTipo().equals(listaTipos.get(listaTipos.size() - 1))) {

                        soma += lista.get(b).getValor();
                        tempoServico = tempo.somaTempo(lista.get(b).getDuracao(), tempoServico);
                        tempoSubTotal = tempo.somaTempo(tempoServico, tempoSubTotal);
                        tempoTotal = tempo.somaTempo(tempoServico, tempoTotal);
                        tipo = listaTipos.get(listaTipos.size() - 1);

                        informacao = new String();
                        informacao = lista.get(b).getDescricaoServico();
                        table.setTotalWidth(80);
                        table.addCell(new Paragraph(informacao));

                        informacao = new String();
                        informacao = tempoServico + " (" + tempo.conversaoHoraMinuto(tempoServico) + ")";
                        table.setTotalWidth(30);
                        table.addCell(new Paragraph(informacao));

                        informacao = new String();
                        informacao = "R$" + df.format(lista.get(b).getValor());
                        table.setTotalWidth(20);
                        table.addCell(new Paragraph(informacao));

                        tempoServico = "00:00:00";
                        //    System.out.println(informacao);                                 
                    }
                }
                //rodap da tabela
                listaTipos.add(tipo);
                valorTotal += soma;

                // subtotal
                table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
                informacao = new String();
                informacao = "Sub Total:";
                table.addCell(informacao);

                // tempo
                informacao = new String();
                informacao = tempoSubTotal + " (" + tempo.conversaoHoraMinuto(tempoSubTotal) + ")";
                table.addCell(informacao);

                // valor
                informacao = new String();
                informacao = "R$" + df.format(soma);
                table.addCell(informacao);
                table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);

                // espao entre tabelas

                table.getDefaultCell().setBorder(1);
                table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                table.getDefaultCell().setColspan(3);
                table.addCell(new Paragraph("* * * * *"));
                table.getDefaultCell().setColspan(0);
                table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
                table.getDefaultCell().setBorder(0);

                tempoSubTotal = "00:00:00";
                //    System.out.println(informacao);   
            }
        }
        // fim da pgina

        // valor total
        informacao = new String();
        informacao = "Total Geral:  ";
        table.getDefaultCell().setBorder(1);
        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(informacao);
        table.getDefaultCell().setBorder(1);

        // tempo
        informacao = new String();
        informacao = tempoTotal + " (" + tempo.conversaoHoraMinuto(tempoTotal) + ")";
        table.addCell(informacao);

        //valor
        informacao = new String();
        informacao = "R$" + df.format(valorTotal);
        table.addCell(informacao);

        doc.add(table);
        //    System.out.println(informacao);

    } finally {
        if (doc != null) {
            //fechamento do documento
            doc.close();
        }
        if (os != null) {
            //fechamento da stream de sada
            os.close();
        }
    }
}