Example usage for com.itextpdf.text Element ALIGN_RIGHT

List of usage examples for com.itextpdf.text Element ALIGN_RIGHT

Introduction

In this page you can find the example usage for com.itextpdf.text Element ALIGN_RIGHT.

Prototype

int ALIGN_RIGHT

To view the source code for com.itextpdf.text Element ALIGN_RIGHT.

Click Source Link

Document

A possible value for paragraph alignment.

Usage

From source file:model.PDFModel.java

License:Open Source License

public void createDailyPDF(String fileName, String date)
        throws DocumentException, FileNotFoundException, BadElementException, IOException {
    Document pdf = new Document();
    PdfWriter.getInstance(pdf, new FileOutputStream(fileName));

    Paragraph tittle = new Paragraph("RESTAURACJA LITERA");
    tittle.setAlignment(Element.ALIGN_CENTER);

    Image logo = Image.getInstance("logo.png");
    logo.setAlignment(Element.ALIGN_RIGHT);

    Paragraph headerParagraph = new Paragraph("Raport Dzienny " + date);
    headerParagraph.setAlignment(Element.ALIGN_CENTER);

    pdf.open();//  w w  w  . j a v  a2 s.  c o m
    pdf.add(logo);
    pdf.add(tittle);
    pdf.add(new Paragraph(" "));
    pdf.add(headerParagraph);
    for (int i = 0; i < 10; i++) {
        pdf.add(createTablePDFTable(i + 1, date));
    }
    pdf.add(createDailyTable(date));
    pdf.close();
    System.out.println("closed");
}

From source file:model.PDFModel.java

License:Open Source License

public PdfPTable createTablePDFTable(int counter, String raportDate) {

    JTable resultTable = query.getProductsInTablePDFTable(raportDate, counter);

    PdfPTable[] tableTable = new PdfPTable[11];
    PdfPCell cell, productNameCell, paymentCell, countCell, priceCell;

    tableTable[counter] = new PdfPTable(4);
    tableTable[counter].setSpacingBefore(5f);
    tableTable[counter].setSpacingAfter(5f);

    totalTablePayment[counter] = 0.00;/*  w w  w  .j ava 2  s . c  o  m*/
    if (resultTable != null) {
        DefaultTableModel resultTableModel = (DefaultTableModel) resultTable.getModel();
        if (resultTableModel.getRowCount() > 0) {
            cell = new PdfPCell(new Phrase("Stolik " + (counter)));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(80, 255, 50));
            tableTable[counter].addCell(cell);
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            tableTable[counter].addCell(cell);
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            tableTable[counter].addCell(cell);
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            tableTable[counter].addCell(cell);

            cell = new PdfPCell(new Phrase("Produkt"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(80, 172, 8));
            tableTable[counter].addCell(cell);
            cell = new PdfPCell(new Phrase("Ilosc"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(80, 172, 8));
            tableTable[counter].addCell(cell);
            cell = new PdfPCell(new Phrase("Cena jednostkowa"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(80, 172, 8));
            tableTable[counter].addCell(cell);
            cell = new PdfPCell(new Phrase("Zaplata"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(80, 172, 8));
            tableTable[counter].addCell(cell);

            for (int i = 0; i < resultTableModel.getRowCount(); i++) {
                productNameCell = new PdfPCell(new Phrase(resultTableModel.getValueAt(i, 0).toString(),
                        FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.NORMAL, BaseColor.BLACK)));
                productNameCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                productNameCell.setBackgroundColor(new BaseColor(80, 135, 8));
                tableTable[counter].addCell(productNameCell);
                countCell = new PdfPCell(new Phrase(String.valueOf(resultTableModel.getValueAt(i, 2))));
                countCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTable[counter].addCell(countCell);
                double payment = Double.parseDouble(resultTableModel.getValueAt(i, 1).toString())
                        * Double.parseDouble(resultTableModel.getValueAt(i, 2).toString());
                totalTablePayment[counter] += payment;
                paymentCell = new PdfPCell(new Phrase(String.valueOf(resultTableModel.getValueAt(i, 1))));
                paymentCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                tableTable[counter].addCell(paymentCell);
                priceCell = new PdfPCell(new Phrase(String.valueOf(payment)));
                priceCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                priceCell.setBackgroundColor(new BaseColor(255, 160, 160));
                tableTable[counter].addCell(priceCell);
            }
        }
    }
    if (totalTablePayment[counter] > 0.00) {
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        tableTable[counter].addCell(cell);
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        tableTable[counter].addCell(cell);
        cell = new PdfPCell(new Phrase("SUMA:",
                FontFactory.getFont(FontFactory.defaultEncoding, 12, Font.NORMAL, BaseColor.BLACK)));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        tableTable[counter].addCell(cell);
        cell = new PdfPCell(new Phrase(String.valueOf(totalTablePayment[counter]),
                FontFactory.getFont(FontFactory.defaultEncoding, 12, Font.BOLD, BaseColor.BLACK)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(new BaseColor(255, 50, 50));
        tableTable[counter].addCell(cell);
    }

    return tableTable[counter];
}

From source file:modelo.crearPdf.java

public String pdf(int tiquet, int idOrden, String codOrden) {
    try {/*from www  .j ava  2 s  .  c om*/

        String sql = "Select * from tiquet where idorden = '" + idOrden + "' and tiquet='" + tiquet + "'";
        rs = Consultar(sql);
        int id = 0;
        int numTiquet = 0;
        String ob = "";
        tiquet verTiquet = new tiquet();
        colores verColor = new colores();
        String colorUno = "";
        String colorDos = "";
        System.out.print("id de la orden en el tiquet:" + idOrden);
        ArrayList<String> lista = new ArrayList<String>();
        int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0, c10 = 0, c11 = 0, c12 = 0,
                c13 = 0, c14 = 0, c15 = 0;
        int c16 = 0, c17 = 0, c18 = 0, c19 = 0, c20 = 0, c21 = 0, c22 = 0, c23 = 0, c24 = 0, c25 = 0, c26 = 0,
                c27 = 0, c28 = 0, c29 = 0;
        int c30 = 0, c31 = 0, c32 = 0, c33 = 0;
        String observacion = "";
        String verificacion = "";
        //rs.last();
        int cuantos = rs.getRow();
        //System.out.print("puestos:" + cuantos+ "orden: " + codOrden + "tiquet: " + tiquet);
        //Creamos el CHUNK definiendo su tipo de letra, tamao
        Chunk direccion = new Chunk("Calle 12 # 6-68 Nia Ceci",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Tiq = new Chunk("Tiquet #",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValTiq = new Chunk("" + tiquet,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Tel = new Chunk("Tel: 5783364",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Ref = new Chunk("Ref",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValRef = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Correo = new Chunk("calzadoseiya@gmail.com",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Par = new Chunk("Pares",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValPar = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FechaTiq = new Chunk("Fecha",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FechaTiq2 = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Orden = new Chunk("Orden",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValOrd = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Cor = new Chunk("CORTADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Guar = new Chunk("GUARNICION",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Mon = new Chunk("MONTADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Emp = new Chunk("EMPLANTILLADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk OBSER = new Chunk("OBSERVACION",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Col1 = new Chunk("Color 1",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Col2 = new Chunk("Color 2",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Encabezado = new Chunk("SEIYA",
                FontFactory.getFont(FontFactory.HELVETICA_BOLD, 11, Font.NORMAL, BaseColor.BLACK));
        Chunk num21 = new Chunk("21",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num22 = new Chunk("22",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num23 = new Chunk("23",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num24 = new Chunk("24",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num25 = new Chunk("25",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num26 = new Chunk("26",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num27 = new Chunk("27",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num28 = new Chunk("28",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num29 = new Chunk("29",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num30 = new Chunk("30",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num31 = new Chunk("31",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num32 = new Chunk("32",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num33 = new Chunk("33",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num34 = new Chunk("34",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num35 = new Chunk("35",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num36 = new Chunk("36",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num37 = new Chunk("37",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num38 = new Chunk("38",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num39 = new Chunk("39",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num40 = new Chunk("40",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num41 = new Chunk("41",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num42 = new Chunk("42",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num43 = new Chunk("43",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num44 = new Chunk("44",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));

        //Fecha actual en formato completo:
        //Tue Sep 23 01:18:48 CEST 2014
        Date fechaActual = new Date();

        //Formateando la fecha:
        DateFormat formatoHora = new SimpleDateFormat("HH-mm-ss");
        DateFormat formatoFecha = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat Fecha = new SimpleDateFormat("dd/MM/yyyy");
        //  System.out.println("Fecha: "+formatoFecha.format(fechaActual)+" Son las: "+formatoHora.format(fechaActual));
        String fe = Fecha.format(fechaActual);
        //Directorio destino para las descargas
        File folder = new File("c:\\seiya\\tiquets");

        //Crea el directorio de destino en caso de que no exista
        folder.mkdirs();

        int numeroAleatorio = (int) (Math.random() * 2500 + 1);
        //Nombre del fichero <strong>PDF</strong> Resultante de la ejecucion
        String dir = "C:\\seiya\\tiquets\\Tiquet_" + tiquet + ".pdf";
        // El archivo pdf que vamos a generar
        FileOutputStream fileOutputStream = new FileOutputStream(dir);
        Rectangle pageSize = new Rectangle(300f, 792f); //ancho y alto
        //Creacion del documento con un tamao y unos margenes predeterminados

        Document document = new Document(pageSize, 1, 1, 1, 1);
        // Obtener la instancia del PdfWriter
        PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);

        //Opens the document.
        //You have to open the document before you can begin to add content to the body of the document.
        document.open();
        //**************************************************************

        //Ejemplos de TABLE
        titulo = new Chunk("", FontFactory.getFont(FontFactory.COURIER, 20, Font.UNDERLINE, BaseColor.BLACK));
        document.add(titulo);
        //Aadir tabla 5 columnas
        PdfPTable table = new PdfPTable(15);
        //Aadir CABECERA

        PdfPCell cell = new PdfPCell();
        cell.setColspan(15);
        table.addCell(cell);

        Image image = Image.getInstance("logo.png");
        //    String col = "jhon";
        cell = new PdfPCell(image);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(7);
        table.addCell(cell);
        /*
        cell = new PdfPCell(new Phrase("imagen:"));
        cell.setColspan(4);
        cell.setRowspan(7);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //cell.setBackgroundColor(BaseColor.GRAY);
        table.addCell(cell);
                
        /*table.addCell(createImageCell(IMG1));*/
        cell = new PdfPCell(new Phrase(direccion));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(6);
        cell.setRowspan(2);

        table.addCell(cell);

        while (rs.next()) {

            c1 = rs.getInt(1);
            c2 = rs.getInt(2);
            c3 = rs.getInt(3);
            c4 = rs.getInt(4);
            c5 = rs.getInt(5);
            observacion = rs.getString(6);
            verificacion = rs.getString(7);
            c8 = rs.getInt(8);
            c9 = rs.getInt(9);
            c10 = rs.getInt(10);
            c11 = rs.getInt(11);
            c12 = rs.getInt(12);
            c13 = rs.getInt(13);
            c14 = rs.getInt(14);
            c15 = rs.getInt(15);
            c16 = rs.getInt(16);
            c17 = rs.getInt(17);
            c18 = rs.getInt(18);
            c19 = rs.getInt(19);
            c20 = rs.getInt(20);
            c21 = rs.getInt(21);
            c22 = rs.getInt(22);
            c23 = rs.getInt(23);
            c24 = rs.getInt(24);
            c25 = rs.getInt(25);
            c26 = rs.getInt(26);
            c27 = rs.getInt(27);
            c28 = rs.getInt(28);
            c29 = rs.getInt(29);
            c30 = rs.getInt(30);
            c31 = rs.getInt(31);
            c32 = rs.getInt(32);
            c33 = rs.getInt(33);

            String c01 = " ", c02 = " ", c03 = " ", c04 = " ", c05 = " ", c06 = " ", c07 = " ", c08 = " ",
                    c09 = " ", c010 = " ", c011 = " ", c012 = " ";
            String c013 = " ", c014 = " ", c015 = " ", c016 = " ", c017 = " ", c018 = " ", c019 = " ",
                    c020 = " ", c021 = " ", c022 = " ", c023 = " ", c024 = " ";
            String c025 = " ", c026 = " ", c027 = " ", c028 = " ", c029 = " ", c030 = " ", c031 = " ",
                    c032 = " ", c033 = " ", c034 = " ";
            if (c1 != 0)
                c01 = String.valueOf(c1);
            if (c2 != 0)
                c02 = String.valueOf(c2);
            if (c3 != 0)
                c03 = String.valueOf(c3);
            if (c4 != 0)
                c04 = String.valueOf(c4);
            if (c5 != 0)
                c05 = String.valueOf(c5);
            if (c8 != 0)
                c08 = String.valueOf(c8);
            if (c9 != 0)
                c09 = String.valueOf(c9);
            if (c10 != 0)
                c010 = String.valueOf(c10);
            if (c11 != 0)
                c011 = String.valueOf(c11);
            if (c12 != 0)
                c012 = String.valueOf(c12);
            if (c13 != 0)
                c013 = String.valueOf(c13);
            if (c14 != 0)
                c014 = String.valueOf(c14);
            if (c15 != 0)
                c015 = String.valueOf(c15);
            if (c16 != 0)
                c016 = String.valueOf(c16);
            if (c17 != 0)
                c017 = String.valueOf(c17);
            if (c18 != 0)
                c018 = String.valueOf(c18);
            if (c19 != 0)
                c019 = String.valueOf(c19);
            if (c20 != 0)
                c020 = String.valueOf(c20);
            if (c21 != 0)
                c021 = String.valueOf(c21);
            if (c22 != 0)
                c022 = String.valueOf(c22);
            if (c23 != 0)
                c023 = String.valueOf(c23);
            if (c24 != 0)
                c024 = String.valueOf(c24);
            if (c25 != 0)
                c025 = String.valueOf(c25);
            if (c26 != 0)
                c026 = String.valueOf(c26);
            if (c27 != 0)
                c027 = String.valueOf(c27);
            if (c28 != 0)
                c028 = String.valueOf(c28);
            if (c29 != 0)
                c029 = String.valueOf(c29);
            if (c30 != 0)
                c030 = String.valueOf(c30);
            if (c31 != 0)
                c031 = String.valueOf(c31);
            if (c32 != 0)
                c032 = String.valueOf(c32);
            if (c33 != 0)
                c033 = String.valueOf(c33);

            colorUno = verColor.consultarNombreColorUno(c4);
            colorDos = verColor.consultarNombreColorDos(c5);
            ob = verTiquet.ConsultaObservacion(tiquet);
            Chunk ValCol1 = new Chunk(colorUno,
                    FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
            Chunk ValCol2 = new Chunk(colorDos,
                    FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
            Chunk ValObserv = new Chunk(ob,
                    FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));

            System.out.print("Color Uno: " + colorUno);

            cell = new PdfPCell(new Phrase(Tiq));
            cell.setColspan(3);
            cell.setRowspan(2);
            cell.setVerticalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.GRAY);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("" + tiquet));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(2);
            cell.setRowspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Tel));
            cell.setColspan(6);
            cell.setRowspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.CYAN);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(Ref));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(2);
            cell.setRowspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("" + c8));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(3);
            cell.setRowspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Correo));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(6);
            cell.setRowspan(2);//para eliminar espacio cabecera reemplazo el 2 por 3
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(Par));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(2);
            cell.setRowspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("" + c9));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(3);
            cell.setRowspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(FechaTiq));//espacio intermedio cabecera
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(fe));//espacio intermedio cabecera
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Orden));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(2);
            cell.setRowspan(1);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("" + codOrden));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //cell.setBackgroundColor(BaseColor.ORANGE);
            cell.setColspan(3);
            cell.setRowspan(1);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Cor));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(""));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(11);
            cell.setRowspan(1);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Guar));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(""));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(11);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Mon));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(""));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(11);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Emp));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(""));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(11);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(OBSER));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            cell.setRowspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(ValObserv));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(11);
            cell.setRowspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Col1));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(3);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(ValCol1));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(4);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(Col2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(3);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(ValCol2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(5);
            table.addCell(cell);

            table.addCell(new Phrase(num21));
            table.addCell(new Phrase(num22));
            table.addCell(new Phrase(num23));
            table.addCell(new Phrase(num24));
            table.addCell(new Phrase(num25));
            table.addCell(new Phrase(num26));
            table.addCell(new Phrase(num27));
            table.addCell(new Phrase(num28));
            table.addCell(new Phrase(num29));
            table.addCell(new Phrase(num30));
            table.addCell(new Phrase(num31));
            table.addCell(new Phrase(num32));
            table.addCell(new Phrase(num33));
            table.addCell(new Phrase(num34));
            table.addCell(new Phrase(num35));

            table.addCell("" + c010);
            table.addCell("" + c011);
            table.addCell("" + c012);
            table.addCell("" + c013);
            table.addCell("" + c014);
            table.addCell("" + c015);
            table.addCell("" + c016);
            table.addCell("" + c017);
            table.addCell("" + c018);
            table.addCell("" + c019);
            table.addCell("" + c020);
            table.addCell("" + c021);
            table.addCell("" + c022);
            table.addCell("" + c023);
            table.addCell("" + c024);

            table.addCell(new Phrase(num36));
            table.addCell(new Phrase(num37));
            table.addCell(new Phrase(num38));
            table.addCell(new Phrase(num39));
            table.addCell(new Phrase(num40));
            table.addCell(new Phrase(num41));
            table.addCell(new Phrase(num42));
            table.addCell(new Phrase(num43));
            table.addCell(new Phrase(num44));
            table.addCell(" ");
            table.addCell(" ");
            table.addCell(" ");
            table.addCell(" ");
            table.addCell(" ");
            table.addCell(" ");

            table.addCell("" + c025);
            table.addCell("" + c026);
            table.addCell("" + c027);
            table.addCell("" + c028);
            table.addCell("" + c029);
            table.addCell("" + c030);
            table.addCell("" + c031);
            table.addCell("" + c032);
            table.addCell("" + c033);
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");

            table.setWidthPercentage(100f);
            table.setHorizontalAlignment(Element.ALIGN_RIGHT);

            document.add(table);

            // String cadena="";
            Chunk cadena = null;
            for (int i = 1; i <= 4; i++) {
                //Chunk chunkSeparador =  new Chunk(SEPARADOR);
                // document.add(chunkSeparador);
                // document.add(Chunk.NEWLINE);

                PdfPTable tabla = new PdfPTable(15);
                if (i == 1)
                    cadena = Emp;
                if (i == 2)
                    cadena = Mon;

                if (i == 3) {
                    cadena = Guar;

                }
                if (i == 4)
                    cadena = Cor;

                cell = new PdfPCell(
                        new Phrase("-----------------------------------------------------------------"));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(15);
                cell.setRowspan(1);
                tabla.addCell(cell);

                cell = new PdfPCell(new Phrase(Encabezado));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(5);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase(cadena));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(4);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase(Tiq));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase("" + tiquet));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                tabla.addCell(cell);

                cell = new PdfPCell(new Phrase(Par));
                cell.setHorizontalAlignment(Font.BOLD);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(2);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase("" + c9));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                tabla.addCell(cell);

                cell = new PdfPCell(new Phrase(""));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(4);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase(Ref));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase("" + c8));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                tabla.addCell(cell);

                cell = new PdfPCell(new Phrase(Col1));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase(ValCol1));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(4);
                tabla.addCell(cell);

                cell = new PdfPCell(new Phrase(Col2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                tabla.addCell(cell);
                cell = new PdfPCell(new Phrase(ValCol2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(5);
                tabla.addCell(cell);

                tabla.addCell(new Phrase(num21));
                tabla.addCell(new Phrase(num22));
                tabla.addCell(new Phrase(num23));
                tabla.addCell(new Phrase(num24));
                tabla.addCell(new Phrase(num25));
                tabla.addCell(new Phrase(num26));
                tabla.addCell(new Phrase(num27));
                tabla.addCell(new Phrase(num28));
                tabla.addCell(new Phrase(num29));
                tabla.addCell(new Phrase(num30));
                tabla.addCell(new Phrase(num31));
                tabla.addCell(new Phrase(num32));
                tabla.addCell(new Phrase(num33));
                tabla.addCell(new Phrase(num34));
                tabla.addCell(new Phrase(num35));

                tabla.addCell("" + c010);
                tabla.addCell("" + c011);
                tabla.addCell("" + c012);
                tabla.addCell("" + c013);
                tabla.addCell("" + c014);
                tabla.addCell("" + c015);
                tabla.addCell("" + c016);
                tabla.addCell("" + c017);
                tabla.addCell("" + c018);
                tabla.addCell("" + c019);
                tabla.addCell("" + c020);
                tabla.addCell("" + c021);
                tabla.addCell("" + c022);
                tabla.addCell("" + c023);
                tabla.addCell("" + c024);

                tabla.addCell(new Phrase(num36));
                tabla.addCell(new Phrase(num37));
                tabla.addCell(new Phrase(num38));
                tabla.addCell(new Phrase(num39));
                tabla.addCell(new Phrase(num40));
                tabla.addCell(new Phrase(num41));
                tabla.addCell(new Phrase(num42));
                tabla.addCell(new Phrase(num43));
                tabla.addCell(new Phrase(num44));
                tabla.addCell(" ");
                tabla.addCell(" ");
                tabla.addCell(" ");
                tabla.addCell(" ");
                tabla.addCell(" ");
                tabla.addCell(" ");

                tabla.addCell("" + c025);
                tabla.addCell("" + c026);
                tabla.addCell("" + c027);
                tabla.addCell("" + c028);
                tabla.addCell("" + c029);
                tabla.addCell("" + c030);
                tabla.addCell("" + c031);
                tabla.addCell("" + c032);
                tabla.addCell("" + c033);
                tabla.addCell("");
                tabla.addCell("");
                tabla.addCell("");
                tabla.addCell("");
                tabla.addCell("");
                tabla.addCell("");

                tabla.setWidthPercentage(100f);
                tabla.setHorizontalAlignment(Element.ALIGN_RIGHT);

                document.add(tabla);

            }
            //FIN Ejemplos de TABLE

        }

        document.close();

        return "exito";

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        return "Error al Generar el PDF";
    }

}

From source file:modelo.pdfOrden.java

public String pdf(int tiquet, int idOrden, String codOrden, int numRegTiq) {
    try {//from w  w  w. j av a  2  s .  co m

        int id = 0;
        int numTiquet = 0;
        String ob = "";
        tiquet verTiquet = new tiquet();
        colores verColor = new colores();
        String colorUno = "";
        String colorDos = "";
        System.out.print("id de la orden en el tiquet:" + idOrden);
        ArrayList<String> lista = new ArrayList<String>();
        int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0, c10 = 0, c11 = 0, c12 = 0,
                c13 = 0, c14 = 0, c15 = 0;
        int c16 = 0, c17 = 0, c18 = 0, c19 = 0, c20 = 0, c21 = 0, c22 = 0, c23 = 0, c24 = 0, c25 = 0, c26 = 0,
                c27 = 0, c28 = 0, c29 = 0;
        int c30 = 0, c31 = 0, c32 = 0, c33 = 0;
        String observacion = "";
        String verificacion = "";

        int idCliente = 0;
        String ciu = "";
        String fecped = "";
        String fecent = "";

        for (orden ord : new orden().CargarDatosOrden(codOrden)) {
            idCliente = ord.getCliente();
            ciu = ord.getCiudad();
            fecped = ord.getPedido();
            fecent = ord.getEntrega();
        }

        //rs.last();
        // int cuantos = rs.getRow();
        //System.out.print("puestos:" + cuantos+ "orden: " + codOrden + "tiquet: " + tiquet);
        //Creamos el CHUNK definiendo su tipo de letra, tamao
        Chunk idClient = new Chunk("" + idCliente,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk direccion = new Chunk("Calle 12 # 6-68 Nia Ceci",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Tiq = new Chunk("TK",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValTiq = new Chunk("" + tiquet,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Tel = new Chunk("Tel: 5783364",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Ref = new Chunk("REF",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValRef = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Correo = new Chunk("calzadoseiya@gmail.com",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Cliente = new Chunk("CLIENTE",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FecEnt = new Chunk("FECHA ENTREGA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValFecEnt = new Chunk(fecent,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FecPed = new Chunk("FECHA PEDIDO",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValFecPed = new Chunk(fecped,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FechaTiq2 = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Orden = new Chunk("ORDEN",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValOrd = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Ciudad = new Chunk("CIUDAD",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValCiudad = new Chunk(ciu,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Cant = new Chunk("CANT.",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Mon = new Chunk("MONTADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Emp = new Chunk("EMPLANTILLADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk OBSER = new Chunk("OBSERVACION",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Col1 = new Chunk("Color 1",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Col2 = new Chunk("Color 2",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Encabezado = new Chunk("SEIYA",
                FontFactory.getFont(FontFactory.HELVETICA_BOLD, 11, Font.NORMAL, BaseColor.BLACK));
        Chunk num21 = new Chunk("21",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num22 = new Chunk("22",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num23 = new Chunk("23",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num24 = new Chunk("24",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num25 = new Chunk("25",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num26 = new Chunk("26",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num27 = new Chunk("27",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num28 = new Chunk("28",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num29 = new Chunk("29",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num30 = new Chunk("30",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num31 = new Chunk("31",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num32 = new Chunk("32",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num33 = new Chunk("33",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num34 = new Chunk("34",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num35 = new Chunk("35",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num36 = new Chunk("36",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num37 = new Chunk("37",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num38 = new Chunk("38",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num39 = new Chunk("39",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num40 = new Chunk("40",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num41 = new Chunk("41",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num42 = new Chunk("42",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num43 = new Chunk("43",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num44 = new Chunk("44",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));

        //Fecha actual en formato completo:
        //Tue Sep 23 01:18:48 CEST 2014
        Date fechaActual = new Date();

        //Formateando la fecha:
        DateFormat formatoHora = new SimpleDateFormat("HH-mm-ss");
        DateFormat formatoFecha = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat Fecha = new SimpleDateFormat("dd/MM/yyyy");
        //  System.out.println("Fecha: "+formatoFecha.format(fechaActual)+" Son las: "+formatoHora.format(fechaActual));
        String fe = Fecha.format(fechaActual);
        //Directorio destino para las descargas
        File folder = new File("c:\\seiya\\ordenes");

        //Crea el directorio de destino en caso de que no exista
        folder.mkdirs();

        int numeroAleatorio = (int) (Math.random() * 2500 + 1);
        //Nombre del fichero <strong>PDF</strong> Resultante de la ejecucion
        String dir = "C:\\seiya\\ordenes\\Orden_" + codOrden + ".pdf";
        // El archivo pdf que vamos a generar
        FileOutputStream fileOutputStream = new FileOutputStream(dir);
        Rectangle pageSize = new Rectangle(792f, 612f); //ancho y alto ->tamao carta
        //Creacion del documento con un tamao y unos margenes predeterminados

        Document document = new Document(pageSize, 1, 1, 1, 1);
        // Obtener la instancia del PdfWriter
        PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);

        //Opens the document.
        //You have to open the document before you can begin to add content to the body of the document.
        document.open();
        //**************************************************************

        //Ejemplos de TABLE
        titulo = new Chunk("", FontFactory.getFont(FontFactory.COURIER, 20, Font.UNDERLINE, BaseColor.BLACK));
        document.add(titulo);
        //Aadir tabla 5 columnas
        PdfPTable table = new PdfPTable(40);
        //Aadir CABECERA

        PdfPCell cell = new PdfPCell();

        /*table.addCell(createImageCell(IMG1));*/
        cell = new PdfPCell(new Phrase(Tiq));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Orden));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(codOrden));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Cliente));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(9);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Ciudad));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(FecPed));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(FecEnt));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Ref));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Cant));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        table.addCell(new Phrase(num21));
        table.addCell(new Phrase(num22));
        table.addCell(new Phrase(num23));
        table.addCell(new Phrase(num24));
        table.addCell(new Phrase(num25));
        table.addCell(new Phrase(num26));
        table.addCell(new Phrase(num27));
        table.addCell(new Phrase(num28));
        table.addCell(new Phrase(num29));
        table.addCell(new Phrase(num30));
        table.addCell(new Phrase(num31));
        table.addCell(new Phrase(num32));
        table.addCell(new Phrase(num33));
        table.addCell(new Phrase(num34));
        table.addCell(new Phrase(num35));
        table.addCell(new Phrase(num36));
        table.addCell(new Phrase(num37));
        table.addCell(new Phrase(num38));
        table.addCell(new Phrase(num39));
        table.addCell(new Phrase(num40));
        table.addCell(new Phrase(num41));
        table.addCell(new Phrase(num42));
        table.addCell(new Phrase(num43));

        cell = new PdfPCell(new Phrase(Col1));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Col2));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(OBSER));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(5);
        cell.setRowspan(1);
        table.addCell(cell);
        /*          
                  ArrayList<String> resul;
              resul = nuevoTiquet.CargarComboTiquet(id);//La consulta tiene que retornar un ArrayList
                  Iterator<String> tiqIterator = resul.iterator();
              while(tiqIterator.hasNext()){
                    String valorTiq = tiqIterator.next();
                            
        //System.out.print(valorTiquet+" / ");
                     // this.impOrd.imp(valorTiq, idOrden, idCadOrden, numRegTiq );
              }
                      
              */

        ArrayList<String> resultat;
        resultat = nuevoTiquet.consultarTiquetporOrden(idOrden);
        Iterator<String> tiquetIterator = resultat.iterator();

        while (tiquetIterator.hasNext()) {
            String valorTiquet = tiquetIterator.next();
            System.out.print(valorTiquet + " ------ ");
            String sql = "Select * from tiquet where idorden = '" + idOrden + "' and tiquet='" + valorTiquet
                    + "'";
            rs = Consultar(sql);

            while (rs.next()) {

                c1 = rs.getInt(1);
                c2 = rs.getInt(2);
                c3 = rs.getInt(3);
                c4 = rs.getInt(4);
                c5 = rs.getInt(5);
                observacion = rs.getString(6);
                verificacion = rs.getString(7);
                c8 = rs.getInt(8);
                c9 = rs.getInt(9);
                c10 = rs.getInt(10);
                c11 = rs.getInt(11);
                c12 = rs.getInt(12);
                c13 = rs.getInt(13);
                c14 = rs.getInt(14);
                c15 = rs.getInt(15);
                c16 = rs.getInt(16);
                c17 = rs.getInt(17);
                c18 = rs.getInt(18);
                c19 = rs.getInt(19);
                c20 = rs.getInt(20);
                c21 = rs.getInt(21);
                c22 = rs.getInt(22);
                c23 = rs.getInt(23);
                c24 = rs.getInt(24);
                c25 = rs.getInt(25);
                c26 = rs.getInt(26);
                c27 = rs.getInt(27);
                c28 = rs.getInt(28);
                c29 = rs.getInt(29);
                c30 = rs.getInt(30);
                c31 = rs.getInt(31);
                c32 = rs.getInt(32);
                c33 = rs.getInt(33);

                String c01 = " ", c02 = " ", c03 = " ", c04 = " ", c05 = " ", c06 = " ", c07 = " ", c08 = " ",
                        c09 = " ", c010 = " ", c011 = " ", c012 = " ";
                String c013 = " ", c014 = " ", c015 = " ", c016 = " ", c017 = " ", c018 = " ", c019 = " ",
                        c020 = " ", c021 = " ", c022 = " ", c023 = " ", c024 = " ";
                String c025 = " ", c026 = " ", c027 = " ", c028 = " ", c029 = " ", c030 = " ", c031 = " ",
                        c032 = " ", c033 = " ", c034 = " ";
                if (c1 != 0)
                    c01 = String.valueOf(c1);
                if (c2 != 0)
                    c02 = String.valueOf(c2);
                if (c3 != 0)
                    c03 = String.valueOf(c3);
                if (c4 != 0)
                    c04 = String.valueOf(c4);
                if (c5 != 0)
                    c05 = String.valueOf(c5);
                if (c8 != 0)
                    c08 = String.valueOf(c8);
                if (c9 != 0)
                    c09 = String.valueOf(c9);
                if (c10 != 0)
                    c010 = String.valueOf(c10);
                if (c11 != 0)
                    c011 = String.valueOf(c11);
                if (c12 != 0)
                    c012 = String.valueOf(c12);
                if (c13 != 0)
                    c013 = String.valueOf(c13);
                if (c14 != 0)
                    c014 = String.valueOf(c14);
                if (c15 != 0)
                    c015 = String.valueOf(c15);
                if (c16 != 0)
                    c016 = String.valueOf(c16);
                if (c17 != 0)
                    c017 = String.valueOf(c17);
                if (c18 != 0)
                    c018 = String.valueOf(c18);
                if (c19 != 0)
                    c019 = String.valueOf(c19);
                if (c20 != 0)
                    c020 = String.valueOf(c20);
                if (c21 != 0)
                    c021 = String.valueOf(c21);
                if (c22 != 0)
                    c022 = String.valueOf(c22);
                if (c23 != 0)
                    c023 = String.valueOf(c23);
                if (c24 != 0)
                    c024 = String.valueOf(c24);
                if (c25 != 0)
                    c025 = String.valueOf(c25);
                if (c26 != 0)
                    c026 = String.valueOf(c26);
                if (c27 != 0)
                    c027 = String.valueOf(c27);
                if (c28 != 0)
                    c028 = String.valueOf(c28);
                if (c29 != 0)
                    c029 = String.valueOf(c29);
                if (c30 != 0)
                    c030 = String.valueOf(c30);
                if (c31 != 0)
                    c031 = String.valueOf(c31);
                if (c32 != 0)
                    c032 = String.valueOf(c32);
                if (c33 != 0)
                    c033 = String.valueOf(c33);

                colorUno = verColor.consultarNombreColorUno(c4);
                colorDos = verColor.consultarNombreColorDos(c5);
                //  ob = verTiquet.ConsultaObservacion(tiquet);
                Chunk ValCol1 = new Chunk(colorUno,
                        FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
                Chunk ValCol2 = new Chunk(colorDos,
                        FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
                Chunk ValObserv = new Chunk(ob,
                        FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));

                System.out.print("Color Uno: " + colorUno);
                table.addCell(new Phrase(ValTiq));
                table.addCell("2");
                table.addCell("3");
                table.addCell("4");
                table.addCell("5");
                table.addCell("6");

                table.addCell("7");
                table.addCell("8");
                table.addCell("9");
                table.addCell("10");
                table.addCell("11");
                table.addCell("12");
                table.addCell("13");
                table.addCell("14");
                table.addCell("15");
                table.addCell("16");
                table.addCell("17");
                table.addCell("18");
                table.addCell("18");
                table.addCell("20");
                table.addCell("21");
                table.addCell("22");
                table.addCell("23");

                table.addCell("24");
                table.addCell("25");
                table.addCell("26");
                table.addCell("27");
                table.addCell("28");
                table.addCell("29");
                table.addCell("30");
                table.addCell("31");
                table.addCell("32");
                table.addCell("33");
                table.addCell("34");
                table.addCell("35");

                table.addCell("36");
                table.addCell("37");
                table.addCell("38");
                table.addCell("39");
                table.addCell("40");

                table.setWidthPercentage(100f);
                table.setHorizontalAlignment(Element.ALIGN_RIGHT);

                document.add(table);

            }

        }

        document.close();

        return "exito";

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        return "Error al Generar el PDF";
    }

}

From source file:modelo.pruebapdf.java

public boolean impOrden(int idOrden, String codOrden, int numRegTiq) {
    try {//from  w  w  w  .  j  ava2s. co  m

        int id = 0;
        int numTiquet = 0;
        String ob = "";
        colores verColor = new colores();
        String colorUno = "";
        String colorDos = "";
        ArrayList<String> lista = new ArrayList<String>();
        int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0, c10 = 0, c11 = 0, c12 = 0,
                c13 = 0, c14 = 0, c15 = 0;
        int c16 = 0, c17 = 0, c18 = 0, c19 = 0, c20 = 0, c21 = 0, c22 = 0, c23 = 0, c24 = 0, c25 = 0, c26 = 0,
                c27 = 0, c28 = 0, c29 = 0;
        int c30 = 0, c31 = 0, c32 = 0, c33 = 0;
        String observacion = "";
        int CanTotal = 0;

        int idCliente = 0;
        String ciu = "";
        String fecped = "";
        String fecent = "";

        for (orden ord : new orden().CargarDatosOrden(codOrden)) {
            idCliente = ord.getCliente();
            ciu = ord.getCiudad();
            fecped = ord.getPedido();
            fecent = ord.getEntrega();
        }
        String nomCliente = nuevaOrden.ConsultarNombreCliente(idCliente);

        Chunk idClient = new Chunk(nomCliente,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk direccion = new Chunk("Calle 12 # 6-68 Nia Ceci",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 12, Font.NORMAL, BaseColor.BLACK));
        Chunk Tiq = new Chunk("TK",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValTiq = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Tel = new Chunk("Tel: 5783364",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 12, Font.NORMAL, BaseColor.BLACK));
        Chunk Ref = new Chunk("REF",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValRef = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Correo = new Chunk("calzadoseiya@gmail.com",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 12, Font.NORMAL, BaseColor.BLACK));
        Chunk Cliente = new Chunk("CLIENTE",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FecEnt = new Chunk("FECHA ENTREGA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValFecEnt = new Chunk(fecent,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FecPed = new Chunk("FECHA PEDIDO",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValFecPed = new Chunk(fecped,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk FechaTiq2 = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Orden = new Chunk("ORDEN",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValOrd = new Chunk("",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Ciudad = new Chunk("CIUDAD",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk ValCiudad = new Chunk(ciu,
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Cant = new Chunk("CANT.",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Mon = new Chunk("MONTADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Emp = new Chunk("EMPLANTILLADA",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk OBSER = new Chunk("OBSERVACION",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 10, Font.NORMAL, BaseColor.BLACK));
        Chunk Col1 = new Chunk("Color 1",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Col2 = new Chunk("Color 2",
                FontFactory.getFont(FontFactory.COURIER_BOLD, 8, Font.NORMAL, BaseColor.BLACK));
        Chunk Encabezado = new Chunk("SEIYA",
                FontFactory.getFont(FontFactory.HELVETICA_BOLD, 11, Font.NORMAL, BaseColor.BLACK));
        Chunk num21 = new Chunk("21",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num22 = new Chunk("22",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num23 = new Chunk("23",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num24 = new Chunk("24",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num25 = new Chunk("25",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num26 = new Chunk("26",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num27 = new Chunk("27",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num28 = new Chunk("28",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num29 = new Chunk("29",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num30 = new Chunk("30",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num31 = new Chunk("31",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num32 = new Chunk("32",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num33 = new Chunk("33",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num34 = new Chunk("34",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num35 = new Chunk("35",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num36 = new Chunk("36",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num37 = new Chunk("37",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num38 = new Chunk("38",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num39 = new Chunk("39",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num40 = new Chunk("40",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num41 = new Chunk("41",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num42 = new Chunk("42",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num43 = new Chunk("43",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));
        Chunk num44 = new Chunk("44",
                FontFactory.getFont(FontFactory.COURIER, 9, Font.NORMAL, BaseColor.BLACK));

        File folder = new File("c:\\seiya\\ordenes");
        folder.mkdirs();
        String dir = "C:\\seiya\\ordenes\\Orden_" + codOrden + ".pdf";
        // El archivo pdf que vamos a generar
        FileOutputStream fileOutputStream = new FileOutputStream(dir);
        Rectangle pageSize = new Rectangle(792f, 612f);

        Document document = new Document(pageSize, 1, 1, 1, 1);
        PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
        document.open();

        PdfPTable table = new PdfPTable(40);
        PdfPCell cell = new PdfPCell();

        Image image = Image.getInstance("logo.png");
        //    String col = "jhon";
        cell = new PdfPCell(image);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(7);
        cell.setRowspan(7);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(direccion));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(33);
        cell.setRowspan(2);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Tel));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(33);
        cell.setRowspan(2);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Correo));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(33);
        cell.setRowspan(2);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(" "));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(33);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Tiq));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Orden));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(codOrden));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Cliente));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(idClient));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(9);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Ciudad));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(ValCiudad));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(FecPed));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(ValFecPed));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(FecEnt));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(ValFecEnt));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(4);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Ref));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Cant));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setRowspan(1);
        table.addCell(cell);

        table.addCell(new Phrase(num21));
        table.addCell(new Phrase(num22));
        table.addCell(new Phrase(num23));
        table.addCell(new Phrase(num24));
        table.addCell(new Phrase(num25));
        table.addCell(new Phrase(num26));
        table.addCell(new Phrase(num27));
        table.addCell(new Phrase(num28));
        table.addCell(new Phrase(num29));
        table.addCell(new Phrase(num30));
        table.addCell(new Phrase(num31));
        table.addCell(new Phrase(num32));
        table.addCell(new Phrase(num33));
        table.addCell(new Phrase(num34));
        table.addCell(new Phrase(num35));
        table.addCell(new Phrase(num36));
        table.addCell(new Phrase(num37));
        table.addCell(new Phrase(num38));
        table.addCell(new Phrase(num39));
        table.addCell(new Phrase(num40));
        table.addCell(new Phrase(num41));
        table.addCell(new Phrase(num42));
        table.addCell(new Phrase(num43));

        cell = new PdfPCell(new Phrase(Col1));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(Col2));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(3);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(OBSER));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(5);
        cell.setRowspan(1);
        table.addCell(cell);

        ArrayList<String> resultat;
        resultat = nuevoTiquet.consultarTiquetporOrden(idOrden);
        Iterator<String> tiquetIterator = resultat.iterator();

        while (tiquetIterator.hasNext()) {

            String valorTiquet = tiquetIterator.next();
            System.out.print(valorTiquet + " ------ ");
            int tiq = Integer.parseInt(valorTiquet);
            ArrayList<tiquet> listaDatosTiquet;
            listaDatosTiquet = nuevoTiquet.conten(idOrden, tiq);//La consulta tiene que retornar un ArrayList

            for (tiquet ord : new tiquet().conten(idOrden, tiq)) {
                System.out.print("Referencia: " + nuevoTiquet.ConsultarReferencia(tiq, idOrden));
                c1 = ord.getTiquet();
                c2 = ord.getIdorden();
                observacion = ord.getObservacion();
                c4 = nuevoTiquet.ConsultarReferencia(tiq, idOrden);
                c5 = nuevoTiquet.ConsultarCantidad(tiq, idOrden);
                c8 = ord.getN21();
                c9 = ord.getN22();
                c10 = ord.getN23();
                c11 = ord.getN24();
                c12 = ord.getN25();
                c13 = ord.getN26();
                c14 = ord.getN27();
                c15 = ord.getN28();
                c16 = ord.getN29();
                c17 = ord.getN30();
                c18 = ord.getN31();
                c19 = ord.getN32();
                c20 = ord.getN33();
                c21 = ord.getN34();
                c22 = ord.getN35();
                c23 = ord.getN36();
                c24 = ord.getN37();
                c25 = ord.getN38();
                c26 = ord.getN39();
                c27 = ord.getN40();
                c28 = ord.getN41();
                c29 = ord.getN42();
                c30 = ord.getN43();

                String c01 = " ", c02 = " ", c03 = " ", c04 = " ", c05 = " ", c06 = " ", c07 = " ", c08 = " ",
                        c09 = " ", c010 = " ", c011 = " ", c012 = " ";
                String c013 = " ", c014 = " ", c015 = " ", c016 = " ", c017 = " ", c018 = " ", c019 = " ",
                        c020 = " ", c021 = " ", c022 = " ", c023 = " ", c024 = " ";
                String c025 = " ", c026 = " ", c027 = " ", c028 = " ", c029 = " ", c030 = " ", c031 = " ",
                        c032 = " ", c033 = " ", c034 = " ";
                if (c1 != 0)
                    c01 = String.valueOf(c1);
                if (c2 != 0)
                    c02 = String.valueOf(c2);
                //    if(c3!=0) c03 = String.valueOf(c3);
                if (c4 != 0)
                    c04 = String.valueOf(c4);
                if (c5 != 0) {
                    c05 = String.valueOf(c5);
                    CanTotal += c5;
                }
                if (c8 != 0)
                    c08 = String.valueOf(c8);
                if (c9 != 0)
                    c09 = String.valueOf(c9);
                if (c10 != 0)
                    c010 = String.valueOf(c10);
                if (c11 != 0)
                    c011 = String.valueOf(c11);
                if (c12 != 0)
                    c012 = String.valueOf(c12);
                if (c13 != 0)
                    c013 = String.valueOf(c13);
                if (c14 != 0)
                    c014 = String.valueOf(c14);
                if (c15 != 0)
                    c015 = String.valueOf(c15);
                if (c16 != 0)
                    c016 = String.valueOf(c16);
                if (c17 != 0)
                    c017 = String.valueOf(c17);
                if (c18 != 0)
                    c018 = String.valueOf(c18);
                if (c19 != 0)
                    c019 = String.valueOf(c19);
                if (c20 != 0)
                    c020 = String.valueOf(c20);
                if (c21 != 0)
                    c021 = String.valueOf(c21);
                if (c22 != 0)
                    c022 = String.valueOf(c22);
                if (c23 != 0)
                    c023 = String.valueOf(c23);
                if (c24 != 0)
                    c024 = String.valueOf(c24);
                if (c25 != 0)
                    c025 = String.valueOf(c25);
                if (c26 != 0)
                    c026 = String.valueOf(c26);
                if (c27 != 0)
                    c027 = String.valueOf(c27);
                if (c28 != 0)
                    c028 = String.valueOf(c28);
                if (c29 != 0)
                    c029 = String.valueOf(c29);
                if (c30 != 0)
                    c030 = String.valueOf(c30);

                cell = new PdfPCell(new Phrase(String.valueOf(tiq)));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(2);
                cell.setRowspan(1);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(c04));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(2);
                cell.setRowspan(1);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(c05));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(2);
                cell.setRowspan(1);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(c08));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c09));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c010));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c011));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c012));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c013));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c014));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c015));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c016));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c017));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c018));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c019));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c020));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c021));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c022));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c023));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c024));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c025));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c026));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c027));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c028));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c029));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(c030));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(1);
                cell.setRowspan(1);
                table.addCell(cell);

                colorUno = verColor.consultarNombreColorUno(ord.getIdcoloruno());
                colorDos = verColor.consultarNombreColorDos(ord.getIdcolordos());
                //  ob = verTiquet.ConsultaObservacion(tiquet);
                Chunk ValCol1 = new Chunk(colorUno,
                        FontFactory.getFont(FontFactory.COURIER, 10, Font.NORMAL, BaseColor.BLACK));
                Chunk ValCol2 = new Chunk(colorDos,
                        FontFactory.getFont(FontFactory.COURIER, 10, Font.NORMAL, BaseColor.BLACK));
                Chunk ValObserv = new Chunk(ord.getObservacion(),
                        FontFactory.getFont(FontFactory.COURIER, 10, Font.NORMAL, BaseColor.BLACK));

                cell = new PdfPCell(new Phrase(ValCol1));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(ValCol2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(3);
                cell.setRowspan(1);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase(ValObserv));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setColspan(5);
                cell.setRowspan(1);
                table.addCell(cell);

            }

        }
        cell = new PdfPCell(new Phrase("Cantidad Total de Pares Producidos "));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(15);
        cell.setRowspan(1);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(String.valueOf(CanTotal)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(25);
        cell.setRowspan(1);
        table.addCell(cell);

        table.setWidthPercentage(100f);
        table.setHorizontalAlignment(Element.ALIGN_RIGHT);
        document.add(table);

        document.close();

        return true;
    } catch (Exception x) {
        return false;
    }

}

From source file:naprawa.praca.PracaController.java

public void akcjaDrukuj() {
    mapaUslug = getDaoFactory().getDaoDefect().pobierzUslugi(getConnection(), wybranyDefect);
    FileOutputStream file = null;
    File druk = null;//from w  ww  . j a v  a 2 s  .c om
    try {
        Document document = new Document(PageSize.A5, 0, 0, 0, 0);
        String userPath = System.getProperty("user.home");
        druk = new File(userPath + "/Baks wydruki");
        if (!druk.exists()) {
            druk.mkdirs();
        }

        File wydruk = new File(druk + "/" + wybranyDefect.getInfoNaprawa() + ".pdf");
        if (!wydruk.exists()) {
            try {
                wydruk.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        file = new FileOutputStream(wydruk);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(wydruk));
            document.open();

            PdfPTable table = new PdfPTable(2);
            table.setWidthPercentage(100);
            table.setSpacingBefore(0f);
            table.setSpacingAfter(0f);

            PdfPTable tableO = new PdfPTable(2);
            tableO.setWidthPercentage(100);
            tableO.setSpacingBefore(0f);
            table.setSpacingAfter(0f);

            tableO.addCell(getNewCell(
                    "BAK's Machine\nin. Baej Krzciuk\n26-800 Biaobrzegi\nul. Brzechwy 31\ntel. 509-281-487"));
            Image image = Image.getInstance(getClass().getClassLoader().getResource("baksZ.jpg"));
            tableO.addCell(image);

            table.addCell(tableO);

            PdfPCell cell1 = getNewCell(
                    "Biaobrzegi, dn. " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell1);

            table.addCell(addTableCzesc());
            table.addCell(addTableCzescService("Materiay", RodzajUslugi.MATERIAL));
            table.addCell(addTableCzescService("Naprawa", RodzajUslugi.NAPRAWA));

            PdfPTable tablePodsumowanie = new PdfPTable(3);
            tablePodsumowanie.setWidthPercentage(100);
            int[] width = { 8, 77, 15 };
            tablePodsumowanie.setWidths(width);
            PdfPCell cell = getNewCell("Podsumowanie");
            cell.setColspan(10);
            cell.setBorder(PdfPCell.NO_BORDER);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            tablePodsumowanie.addCell(cell);

            tablePodsumowanie.addCell(getNewCell("1."));
            tablePodsumowanie.addCell(getNewCell("Czci"));
            tablePodsumowanie.addCell(getNewCell(wybranyDefect.getKosztCzesciS() + " z"));

            tablePodsumowanie.addCell(getNewCell("2."));
            tablePodsumowanie.addCell(getNewCell("Materiay"));
            tablePodsumowanie.addCell(getNewCell(wybranyDefect.getKosztMaterialyS() + " z"));

            tablePodsumowanie.addCell(getNewCell("3."));
            tablePodsumowanie.addCell(getNewCell("Naprawa"));
            tablePodsumowanie.addCell(getNewCell(wybranyDefect.getKosztNaprawyS() + " z"));

            tablePodsumowanie.addCell(getNewCell(""));
            tablePodsumowanie.addCell(getNewCell("RAZEM"));
            tablePodsumowanie.addCell(getNewCell(wybranyDefect.getKosztSumaS() + " z"));

            table.addCell(tablePodsumowanie);

            document.add(table);

            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex);
        BaksSessionBean.getInstance().fireMessage(widok, "Wydruk",
                "Pdf do ktrego chcesz zapisa wynik jest otwarty!\n Zamknij i sprbuj jeszcze raz.");
    } finally {
        try {
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    Desktop desktop = null;
    if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
        try {
            desktop.open(druk);
        } catch (IOException ex) {
            Logger.getLogger(WyszukPlatnosciController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    BaksSessionBean.getInstance().fireMessage(widok, "Zapis",
            "Wydruk zapisany w folderze: " + System.getProperty("user.home") + "/Baks wydruki");
}

From source file:net.sf.texprinter.generators.PDFGenerator.java

License:Open Source License

/**
 * Generates a PDF file from a Question object.
 * /*from  ww  w .ja  v  a  2  s .c  o m*/
 * @param question The question.
 * @param filename The filename.
 */
public static void generate(Question question, String filename) {

    // wait window
    ProgressMessage pm = new ProgressMessage("TeXPrinter is printing your PDF file.");

    // start wait window
    //pm.start();

    // log message
    log.log(Level.INFO, "Starting PDF generation of {0}.", filename);

    // define a new PDF document
    Document document = null;

    // define a new PDF writer
    PdfWriter writer = null;

    // lets try
    try {

        // create a new PDF document
        document = new Document();

        // define a new PDF Writer
        writer = PdfWriter.getInstance(document, new FileOutputStream(filename));

        // set the PDF version
        writer.setPdfVersion(PdfWriter.VERSION_1_6);

        // open the document
        document.open();

        // set the title font
        Font titleFont = new Font(FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLACK);

        // set the chunk for the question title
        Chunk questionTitle = new Chunk(question.getQuestion().getTitle(), titleFont);

        // create a paragraph from that chunk
        Paragraph paragraphQuestionTitle = new Paragraph(questionTitle);

        // log message
        log.log(Level.INFO, "Adding the question title.");

        // add the question title to the document
        document.add(paragraphQuestionTitle);

        // set the asker font
        Font askerFont = new Font(FontFamily.HELVETICA, 10, Font.ITALIC, BaseColor.DARK_GRAY);

        // set the chunk for the asker
        Chunk questionAsker = new Chunk("Asked by " + question.getQuestion().getUser().getName() + " ("
                + question.getQuestion().getUser().getReputation() + ") on " + question.getQuestion().getDate()
                + " (" + String.valueOf(question.getQuestion().getVotes())
                + (question.getQuestion().getVotes() == 1 ? " vote" : " votes") + ")", askerFont);

        // create a paragraph from that chunk
        Paragraph paragraphQuestionAsker = new Paragraph(questionAsker);

        // log message
        log.log(Level.INFO, "Adding both asker and reputation.");

        // add the asker to the document
        document.add(paragraphQuestionAsker);

        // create a line separator
        LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -5);

        // add the line to the document
        document.add(line);

        // add a new line
        document.add(Chunk.NEWLINE);

        // create a list of elements from the question objects
        List<Element> questionTextObjects = getPostText(question.getQuestion().getText());

        // log message
        log.log(Level.INFO, "Adding the question text.");

        // for each element
        for (Element questionTextObject : questionTextObjects) {

            // add it to the document
            document.add(questionTextObject);
        }

        // add a new line
        document.add(Chunk.NEWLINE);

        // create a new font for the comments title
        Font commentsTitleFont = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);

        // create a new chunk based on that font
        Chunk commentsTitle = new Chunk(
                "This question has " + question.getQuestion().getComments().size()
                        + ((question.getQuestion().getComments().size() == 1) ? " comment:" : " comments:"),
                commentsTitleFont);

        // create a paragraph from that chunk
        Paragraph paragraphCommentsTitle = new Paragraph(commentsTitle);

        // if there are comments to this question
        if (!question.getQuestion().getComments().isEmpty()) {

            // log message
            log.log(Level.INFO, "Adding the question comments.");

            // add that paragraph to the document
            document.add(paragraphCommentsTitle);

            // add a new line
            document.add(Chunk.NEWLINE);

            // get all the comments
            List<Comment> questionComments = question.getQuestion().getComments();

            // for each comment
            for (Comment questionComment : questionComments) {

                // get the elements of the comment text
                List<Element> questionCommentObjects = getPostText(questionComment.getText());

                // for each element
                for (Element questionCommentObject : questionCommentObjects) {

                    // add it to the document
                    document.add(questionCommentObject);
                }

                // create a new paragraph about the comment author
                Paragraph paragraphCommentAuthor = new Paragraph(questionComment.getAuthor() + " on "
                        + questionComment.getDate() + " (" + String.valueOf(questionComment.getVotes())
                        + (questionComment.getVotes() == 1 ? " vote" : " votes") + ")", askerFont);

                // set the alignment to the right
                paragraphCommentAuthor.setAlignment(Element.ALIGN_RIGHT);

                // add the paragraph to the document
                document.add(paragraphCommentAuthor);

                // add a new line
                document.add(Chunk.NEWLINE);
            }
        }

        // add a line separator
        document.add(line);

        // add two new lines
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);

        // get the list of answers
        List<Post> answersList = question.getAnswers();

        // if there are no answers
        if (answersList.isEmpty()) {

            // log message
            log.log(Level.INFO, "This question has no answers.");

            // create a new chunk
            Chunk noAnswersTitle = new Chunk("Sorry, this question has no answers yet.", titleFont);

            // create a paragraph from that chunk
            Paragraph paragraphNoAnswersTitle = new Paragraph(noAnswersTitle);

            // add the paragraph to the document
            document.add(paragraphNoAnswersTitle);

        } else {

            // log message
            log.log(Level.INFO, "Adding answers.");

            // there are answers, so create a counter for answers
            int answerCount = 1;

            // for each answer
            for (Post answer : answersList) {

                // log message
                log.log(Level.INFO, "Adding answer {0}.", answerCount);

                // set the message text as empty
                String answerAccepted = "";

                // if the answer is accepted
                if (answer.isAccepted()) {

                    // add that to the message
                    answerAccepted = " - Marked as accepted.";
                }

                // create a new chunk
                Chunk answerTitle = new Chunk("Answer #" + answerCount, titleFont);

                // create a paragraph from that chunk
                Paragraph paragraphAnswerTitle = new Paragraph(answerTitle);

                // add the paragraph to the document
                document.add(paragraphAnswerTitle);

                // increase the counter
                answerCount++;

                // create a new chunk
                Chunk questionAnswerer = new Chunk("Answered by " + answer.getUser().getName() + " ("
                        + answer.getUser().getReputation() + ") on " + answer.getDate() + answerAccepted + " ("
                        + String.valueOf(answer.getVotes()) + (answer.getVotes() == 1 ? " vote" : " votes")
                        + ")", askerFont);

                // create a paragraph from that chunk
                Paragraph paragraphQuestionAnswerer = new Paragraph(questionAnswerer);

                // add that paragraph to the document
                document.add(paragraphQuestionAnswerer);

                // add a line separator
                document.add(line);

                // add a new line
                document.add(Chunk.NEWLINE);

                // create a list of elements from the answer text
                List<Element> answerTextObjects = getPostText(answer.getText());

                // for each element
                for (Element answerTextObject : answerTextObjects) {

                    // add it to the document
                    document.add(answerTextObject);
                }

                // add a new line
                document.add(Chunk.NEWLINE);

                // create a new font style
                Font answerCommentsTitleFont = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);

                // create a new chunk
                Chunk answerCommentsTitle = new Chunk(
                        "This answer has " + answer.getComments().size()
                                + ((answer.getComments().size() == 1) ? " comment:" : " comments:"),
                        answerCommentsTitleFont);

                // create a paragraph from that chunk
                Paragraph paragraphAnswerCommentsTitle = new Paragraph(answerCommentsTitle);

                // if there are comments for that answer
                if (!answer.getComments().isEmpty()) {

                    // log message
                    log.log(Level.INFO, "Adding comments for answer {0}.", (answerCount - 1));

                    // add that paragraph to the document
                    document.add(paragraphAnswerCommentsTitle);

                    // add a new line
                    document.add(Chunk.NEWLINE);

                    // get all the comments
                    List<Comment> answerComments = answer.getComments();

                    // for each comment
                    for (Comment answerComment : answerComments) {

                        // create a list of elements from the comment text
                        List<Element> answerCommentObjects = getPostText(answerComment.getText());

                        // for each element
                        for (Element answerCommentObject : answerCommentObjects) {

                            // add it to the document
                            document.add(answerCommentObject);
                        }

                        // create a new paragraph for the comment author
                        Paragraph paragraphAnswerCommentAuthor = new Paragraph(
                                answerComment.getAuthor() + " on " + answerComment.getDate() + " ("
                                        + String.valueOf(answerComment.getVotes())
                                        + (answerComment.getVotes() == 1 ? " vote" : " votes") + ")",
                                askerFont);

                        // set the aligment to the right
                        paragraphAnswerCommentAuthor.setAlignment(Element.ALIGN_RIGHT);

                        // add the paragraph to the document
                        document.add(paragraphAnswerCommentAuthor);

                        // add a new line
                        document.add(Chunk.NEWLINE);
                    }
                }

                // add a line separator
                document.add(line);

                // add two new lines
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
            }
        }

        // log message
        log.log(Level.INFO, "PDF generation complete, closing {0}.", filename);

        // close the document
        document.close();

        // stop wait window
        pm.interrupt();

    } catch (IOException ioexception) {

        // stop wait window
        pm.interrupt();

        // log message
        log.log(Level.SEVERE, "An IO error occurred while trying to create the PDF file. MESSAGE: {0}",
                StringUtils.printStackTrace(ioexception));

        // critical error, exit
        Dialogs.showExceptionWindow();

    } catch (Exception exception) {

        // log message
        log.log(Level.SEVERE, "A generic error occurred while trying to create the PDF file. MESSAGE: {0}",
                StringUtils.printStackTrace(exception));

        // log message
        log.log(Level.INFO, "I will try to remove the remaining PDF file.");

        try {

            // log message
            log.log(Level.INFO, "Closing both document and writer.");

            // close the document
            document.close();

            // close the writer
            writer.close();

        } catch (Exception ex) {

            // log message
            log.log(Level.WARNING, "I could not close either document or writer. MESSAGE: {0}",
                    StringUtils.printStackTrace(ex));
        }

        try {

            // reference problematic file
            File target = new File(filename);

            // log message
            log.log(Level.INFO, "Opening problematic file {0}.", filename);

            // check if file exists
            if (target.exists()) {

                // log message
                log.log(Level.INFO, "File exists, trying to delete it.");

                // trying to remove it
                if (target.delete()) {

                    // log message
                    log.log(Level.INFO, "File {0} was successfully removed.", filename);

                } else {

                    // log message
                    log.log(Level.SEVERE, "File {0} could not be removed.", filename);

                }
            }
        } catch (SecurityException se) {

            // log message
            log.log(Level.SEVERE, "A security exception was raised. MESSAGE: {0}",
                    StringUtils.printStackTrace(se));

        }

        // stop wait window
        pm.interrupt();

        // critical error, exit
        Dialogs.showExceptionWindow();

    }
}

From source file:net.viralpatel.pdf.FirstPdf.java

public static void main(String[] args) {
    try {/*from  w  w  w.  ja v a2  s  . c om*/
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        // Left
        Paragraph paragraph = new Paragraph("This is right aligned text");
        paragraph.setAlignment(Element.ALIGN_RIGHT);
        document.add(paragraph);
        // Centered
        paragraph = new Paragraph("This is centered text");
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);
        // Left
        paragraph = new Paragraph("This is left aligned text");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);
        // Left with indentation
        paragraph = new Paragraph("This is left aligned text with indentation");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setIndentationLeft(50);
        document.add(paragraph);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:nl.avans.C3.BusinessLogic.InvoiceService.java

public void generateInvoice(String invoiceBSN, int[] behandelCode)
        throws DocumentException, FileNotFoundException, ClientNotFoundException, ParseException {
    Date date = new Date();
    String dt = new SimpleDateFormat("yyyy-MM-dd").format(date);
    String fileId = "invoice-" + dt + "-" + invoiceBSN;

    String companyName = companyService.getInsuranceCompany().getName();
    String companyAddress = companyService.getInsuranceCompany().getAddress();
    String companyPostalCode = companyService.getInsuranceCompany().getPostalCode();
    String companyCity = companyService.getInsuranceCompany().getCity();

    String companyKVK = Integer.toString(companyService.getInsuranceCompany().getKVK());
    String companyIBAN = "NL91ABNA0417164300";

    Client client = clientService.findClientByBSN(Integer.parseInt(invoiceBSN));
    String clientFirstName = client.getFirstName();
    String clientLastName = client.getLastName();
    String clientAddress = client.getAddress();
    String clientPostalCode = client.getPostalCode();
    String clientCity = client.getCity();
    boolean incasso = client.isIncasso();

    Date date2 = new Date();
    String dt2 = new SimpleDateFormat("dd-MM-yyyy").format(date2);
    String currentDate = dt2;//from www .  j a v  a2s  . c o  m
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(dt2));
    c.add(Calendar.MONTH, 1);
    dt2 = sdf.format(c.getTime());
    String expirationDate = dt2;

    String invoiceNumber = dt + "-" + invoiceBSN;

    Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
    Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

    // Creating new file
    Document doc = new Document();

    // Trying to parse the data to a new PDF
    PdfWriter.getInstance(doc, new FileOutputStream("generatedfiles/invoice/" + fileId + ".pdf"));

    // Opening the file
    doc.open();

    // Parsing the order details to the PDF
    doc.add(new Paragraph(companyName + "\n" + companyAddress + "\n" + companyPostalCode + " " + companyCity
            + "\n\n" + "KVK: " + companyKVK + "\nIBAN: " + companyIBAN + "\n\n\n" + clientFirstName + " "
            + clientLastName + "\n" + clientAddress + "\n" + clientPostalCode + " " + clientCity + "\n\n"
            + "Factuurdatum: " + currentDate + "\nVerloopdatum: " + expirationDate + "\n\n\n"
            + "Factuurnummer: " + invoiceNumber + "\n\n\n\n"));

    float[] columnWidths = { 2f, 2f, 2f, 2f, 2f };
    PdfPTable table = new PdfPTable(columnWidths);
    table.setWidthPercentage(100f);

    insertCell(table, "Behandelcode", Element.ALIGN_LEFT, 1, bfBold12);
    insertCell(table, "Behandelingnaam", Element.ALIGN_LEFT, 1, bfBold12);
    insertCell(table, "Aantal sessies", Element.ALIGN_LEFT, 1, bfBold12);
    insertCell(table, "Sessieduur", Element.ALIGN_LEFT, 1, bfBold12);
    insertCell(table, "Tarief", Element.ALIGN_LEFT, 1, bfBold12);
    table.setHeaderRows(1);

    List<Treatment> treatments = getTreatments(behandelCode);

    for (int i = 0; i < treatments.size(); i++) {
        insertCell(table, "" + treatments.get(i).getBehandelCode(), Element.ALIGN_LEFT, 1, bf12);
        insertCell(table, "" + treatments.get(i).getBehandelingNaam(), Element.ALIGN_LEFT, 1, bf12);
        insertCell(table, "" + treatments.get(i).getAantalSessies(), Element.ALIGN_LEFT, 1, bf12);
        insertCell(table, "" + treatments.get(i).getSessieDuur(), Element.ALIGN_LEFT, 1, bf12);
        insertCell(table, "" + treatments.get(i).getTariefBehandeling(), Element.ALIGN_RIGHT, 1, bf12);
    }

    //totaalbedrag zonder eigen risico
    double totaalBedrag = getTotaalBedrag(behandelCode);
    insertCell(table, "Totaalbedrag: ", Element.ALIGN_RIGHT, 4, bfBold12);
    insertCell(table, "" + totaalBedrag, Element.ALIGN_RIGHT, 1, bfBold12);

    //lege row
    insertCell(table, "", Element.ALIGN_RIGHT, 5, bf12);

    //huidig eigen risico
    double excess = insuranceContractService.getInsuranceContractByBSN(Integer.parseInt(invoiceBSN))
            .getExcess();
    insertCell(table, "Huidig eigen risico: ", Element.ALIGN_RIGHT, 4, bf12);
    insertCell(table, "" + excess, Element.ALIGN_RIGHT, 1, bf12);

    //totaal te betalen bedrag
    double teBetalenBedrag;
    double newExcess = excess - totaalBedrag;
    insertCell(table, "Te betalen bedrag: ", Element.ALIGN_RIGHT, 4, bfBold12);
    if (excess > 0) {
        if (excess > totaalBedrag) {
            insuranceContractService.updateInsuranceContractExcess(newExcess, insuranceContractService
                    .getInsuranceContractByBSN(Integer.parseInt(invoiceBSN)).getInsuranceContractID());
            insertCell(table, "" + totaalBedrag, Element.ALIGN_RIGHT, 1, bfBold12);
            teBetalenBedrag = totaalBedrag;
        } else {
            insuranceContractService.updateInsuranceContractExcess(0.00, insuranceContractService
                    .getInsuranceContractByBSN(Integer.parseInt(invoiceBSN)).getInsuranceContractID());
            insertCell(table, "" + excess, Element.ALIGN_RIGHT, 1, bfBold12);
            teBetalenBedrag = excess;
        }
    } else {
        insertCell(table, "0.0", Element.ALIGN_RIGHT, 1, bfBold12);
        teBetalenBedrag = 0.00;
    }

    Paragraph paragraph = new Paragraph("");
    paragraph.add(table);
    doc.add(paragraph);

    doc.add(new Paragraph("\n\n"));

    if (newExcess > 0) {
        doc.add(new Paragraph("U heeft nog " + newExcess + " eigen risico over."));
    } else {
        doc.add(new Paragraph("U heeft nog 0.0 eigen risico over."));
    }

    doc.add(new Paragraph("\n\n"));
    if (incasso == false) {
        if (teBetalenBedrag > 0) {
            doc.add(new Paragraph("We verzoeken u vriendelijk het bovenstaande bedrag van " + teBetalenBedrag
                    + " voor " + expirationDate
                    + " te voldoen op onze bankrekening onder vermelding van het factuurnummer " + invoiceNumber
                    + ". Voor vragen kunt u contact opnemen per e-mail."));
        } else {
            doc.add(new Paragraph(
                    "Omdat uw eigen risico op is worden er geen kosten in rekening gebracht voor de bovenstaande behandelingen."));
        }
    } else {
        doc.add(new Paragraph(
                "Het geld zal binnen 10 werkdagen van uw rekening afgeschreven worden door middel van een automatische incasso."));
    }

    // Closing the file
    doc.close();
}

From source file:nl.ctmm.trait.proteomics.qcviewer.utils.ReportPDFExporter.java

License:Apache License

/**
 * Add a metric to the pdf table./*from w  ww . j  a  v  a  2  s.  com*/
 *
 * @param key           the metric key.
 * @param description   the metric description.
 * @param metricsValues the metric values.
 * @param table         the pdf table.
 */
private static void addMetric(final String key, final String description,
        final Map<String, String> metricsValues, final PdfPTable table) {
    final String value = (metricsValues != null) ? metricsValues.get(key) : Constants.NOT_AVAILABLE_STRING;
    // Populate content in table cell.
    table.addCell(new Phrase(key, Constants.TABLE_CONTENT_FONT));
    table.addCell(new Phrase(description, Constants.TABLE_CONTENT_FONT));
    final PdfPCell valueCell = new PdfPCell(new Phrase(value, Constants.TABLE_CONTENT_FONT));
    valueCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(valueCell);
}