Example usage for com.itextpdf.text Document Document

List of usage examples for com.itextpdf.text Document Document

Introduction

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

Prototype


public Document() 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:alessandrafx.MarcoVentanaController.java

@FXML
private void generarReporteMenusal(MouseEvent event) {

    try {/* w  w w.j av  a  2 s  .  c  om*/
        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialFileName("Reporte.pdf");
        File file = fileChooser.showSaveDialog(null);
        file.getParentFile().mkdir();

        Document document = new Document();
        try {
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("Factura emitida el " + new Date()));
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        SistemaAleMC sistema = new SistemaAleMC();
        Double balanceMonetario = 0.0;

        //Para salidas de dinero
        ArrayList<Capital> capitales = sistema.getRegistrosCapitalTipo('S');
        cell = new PdfPCell(new Phrase("Gastos y pagos realizados"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        //Agrega los registros de salidas de dinero
        Double totalEgreso = 0.0;
        if (capitales.isEmpty()) {
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        for (Capital capital : capitales) {
            cell = new PdfPCell(new Phrase(capital.getMotivo().toString()));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(capital.getMonto().toString()));
            balanceMonetario -= Double.valueOf(capital.getMonto());
            totalEgreso -= Double.valueOf(capital.getMonto());
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        cell = new PdfPCell(new Phrase("TOTAL DE EGRESOS"));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(totalEgreso.toString()));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //Para Pago de mensualidades
        capitales = sistema.getRegistrosCapitalTipo('M');
        cell = new PdfPCell(new Phrase("Pagos de mensualidad recibidos"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        //Agrega los registros de pago de mensualidades de los alumnos
        Double totalIngresoMen = 0.0;
        if (capitales.isEmpty()) {
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        for (Capital capital : capitales) {
            cell = new PdfPCell(new Phrase(capital.getMotivo().toString()));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(capital.getMonto().toString()));
            balanceMonetario += Double.valueOf(capital.getMonto());
            totalIngresoMen += Double.valueOf(capital.getMonto());
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        cell = new PdfPCell(new Phrase("TOTAL DE MENSUALIDADES"));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(totalIngresoMen.toString()));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //Para Pago de anualidades
        capitales = sistema.getRegistrosCapitalTipo('A');
        cell = new PdfPCell(new Phrase("Pagos de anualidades recibidos"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        //Agrega los registros de pago de anualidades de los alumnos
        Double totalIngresoAnu = 0.0;
        if (capitales.isEmpty()) {
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(" --- "));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        for (Capital capital : capitales) {
            cell = new PdfPCell(new Phrase(capital.getMotivo().toString()));
            cell.setColspan(2);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(capital.getMonto().toString()));
            balanceMonetario += Double.valueOf(capital.getMonto());
            totalIngresoAnu += Double.valueOf(capital.getMonto());
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell);
        }
        cell = new PdfPCell(new Phrase("TOTAL DE ANUALIDADES"));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(totalIngresoAnu.toString()));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //PARA EL BALANCE MONETARIO
        capitales = sistema.getRegistrosCapitalTipo('A');
        cell = new PdfPCell(new Phrase("Total"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        //Agrega la celda del total monetario
        if (balanceMonetario == (Double) 0.0) {
            cell = new PdfPCell(new Phrase("No hubieron movimientos este mes"));
        } else {
            cell = new PdfPCell(new Phrase(balanceMonetario.toString()));
        }
        cell.setColspan(3);
        cell.setBackgroundColor(BaseColor.WHITE);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        try {
            document.add(table);
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        document.close();
        Alert alerta = new Alert(Alert.AlertType.INFORMATION);
        alerta.setTitle("Corre a ver tu PDF!");
        alerta.setHeaderText(null);
        alerta.setContentText("El PDF ha sido generado, puedes revisarlo");
        alerta.showAndWait();

    } catch (Exception e) {
        Alert alerta = new Alert(Alert.AlertType.INFORMATION);
        alerta.setTitle("Lo sentimos");
        alerta.setHeaderText(null);
        alerta.setContentText("La ubicacin seleccionada no es vlida y por tanto, no se generar el PDF");
        alerta.showAndWait();
    }
}

From source file:alessandrafx.VentanaEgresoController.java

private void generarReportePagoColaborador(int pagoColaborador, int pagoSugerido) throws DatoFaltante {
    try {//w  w  w .  j  ava 2s. c  o  m
        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialFileName("facturaNueva.pdf");
        File file = fileChooser.showSaveDialog(null);
        file.getParentFile().mkdir();

        Document document = new Document();
        try {
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("Factura emitida el " + new Date()));
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        //Agrega el pago IDEAL
        cell = new PdfPCell(new Phrase("Pago sugerido recibido"));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Pago sugerido"));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //Agrega el pago realizado
        cell = new PdfPCell(new Phrase("Pago realizado"));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(String.valueOf(pagoColaborador)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);
        try {
            document.add(table);
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        document.close();

    } catch (Exception e) {
        throw new DatoFaltante("ubicacin no vlida", "La ubicacin que has escogido no es vlida");
    }

}

From source file:alessandrafx.VentanaEgresoController.java

private void generarReporteOtroGasto(int cantidad, String motivo) throws DatoFaltante {
    try {/* www  .  j a va  2 s  .com*/
        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialFileName("facturaHecha.pdf");
        File file = fileChooser.showSaveDialog(null);
        file.getParentFile().mkdir();

        Document document = new Document();
        try {
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("Factura emitida el " + new Date()));
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        //Agrega el monto pagado
        cell = new PdfPCell(new Phrase("Pago realizado"));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(String.valueOf(cantidad)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //Agrega el motivo del pago
        cell = new PdfPCell(new Phrase("Motivo: \n" + motivo));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(3);
        table.addCell(cell);
        try {
            document.add(table);
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        document.close();

    } catch (Exception e) {
        throw new DatoFaltante("ubicacin no vlida", "La ubicacin que has escogido no es vlida");
    }

}

From source file:alessandrafx.VentanaIngresoController.java

public void generarReportePago(String pagoSugerido, String pagoRealizado) throws DatoFaltante {

    try {//from   w w  w . j  a  v  a  2  s  . com
        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialFileName("facturaNueva.pdf");
        File file = fileChooser.showSaveDialog(null);
        file.getParentFile().mkdir();

        Document document = new Document();
        try {
            try {
                PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("Factura emitida el " + new Date()));
        cell.setColspan(3);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        //Agrega el pago IDEAL
        cell = new PdfPCell(new Phrase("Pago sugerido recibido"));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(pagoSugerido));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);

        //Agrega el pago realizado
        cell = new PdfPCell(new Phrase("Pago realizado"));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(String.valueOf(pagoRealizado)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);
        try {
            document.add(table);
        } catch (DocumentException ex) {
            Logger.getLogger(VentanaEgresoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        document.close();

    } catch (Exception e) {
        throw new DatoFaltante("ubicacin no vlida", "La ubicacin que has escogido no es vlida");
    }
}

From source file:analizadorventas.controlador.ControladorPdf.java

/**
  * @param lista //ww w .  ja  va  2 s . c om
  */
public static void CrearPdf(List<Transaccion> lista) {
    try {
        Document documento = new Document();
        PdfWriter.getInstance(documento, new FileOutputStream(new File("acercaDe.pdf")));
        documento.open();
        PdfPTable fila = new PdfPTable(5);
        fila.setHeaderRows(1);
        for (String cadena : header) {
            fila.addCell(cadena);
        }
        for (Transaccion transaccion : lista) {
            fila.setSpacingAfter(10);
            fila.addCell(transaccion.getNombreCliente());
            fila.addCell(transaccion.getProductoComprado());
            fila.addCell(transaccion.getPrecio() + "");
            fila.addCell(transaccion.getFecha());
            fila.addCell(transaccion.getCiudad());
        }
        documento.add(fila);
        documento.close();
    } catch (DocumentException | FileNotFoundException ex) {
        Logger.getLogger(ControladorPdf.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:app.logica.gestores.GestorPDF.java

License:Open Source License

/**
  * Mtodo para crear un PDF a partir de una pantalla.
  */*  w ww.ja  v  a  2  s. c o  m*/
  * @param pantallaAPDF
  *            pantalla que se imprimir en PDF
  * @return PDF de una captura de la pantalla pasada
  */
 private PDF generarPDF(Node pantallaAPDF) throws Exception {
     //Se imprime la pantalla en una imagen
     new Scene((Parent) pantallaAPDF);
     WritableImage image = pantallaAPDF.snapshot(new SnapshotParameters(), null);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", baos);
     byte[] imageInByte = baos.toByteArray();
     baos.flush();
     baos.close();

     //Se carga la imagen en un PDF
     Image imagen = Image.getInstance(imageInByte);
     Document document = new Document();
     ByteArrayOutputStream pdfbaos = new ByteArrayOutputStream();
     PdfWriter escritor = PdfWriter.getInstance(document, pdfbaos);
     document.open();
     imagen.setAbsolutePosition(0, 0);
     imagen.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
     document.add(imagen);
     document.close();

     //Se obtiene el archivo PDF
     byte[] pdfBytes = pdfbaos.toByteArray();
     pdfbaos.flush();
     escritor.close();
     pdfbaos.close();

     //Se genera un objeto PDF
     return (PDF) new PDF().setArchivo(pdfBytes);
 }

From source file:app.logica.gestores.GestorPDF.java

License:Open Source License

/**
  * Mtodo para crear un PDF a partir de varias pantalla.
  *//from  www .j  av a2s  .  c o  m
  * @param pantallaAPDF
  *            pantalla que se imprimir en PDF
  * @return PDF de una captura de la pantalla pasada
  */
 private PDF generarPDF(ArrayList<Node> pantallasAPDF) throws Exception {
     Document document = new Document();
     ByteArrayOutputStream pdfbaos = new ByteArrayOutputStream();
     PdfWriter escritor = PdfWriter.getInstance(document, pdfbaos);
     document.open();

     for (Node pantalla : pantallasAPDF) {
         new Scene((Parent) pantalla);
         WritableImage image = pantalla.snapshot(new SnapshotParameters(), null);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", baos);
         byte[] imageInByte = baos.toByteArray();
         baos.flush();
         baos.close();
         Image imagen = Image.getInstance(imageInByte);
         imagen.setAbsolutePosition(0, 0);
         imagen.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
         document.add(imagen);
         document.newPage();
     }

     document.close();

     byte[] pdfBytes = pdfbaos.toByteArray();
     pdfbaos.flush();
     escritor.close();
     pdfbaos.close();
     return (PDF) new PDF().setArchivo(pdfBytes);
 }

From source file:appArriendos.frmReservaEquipo.java

private void btnExportarApdfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportarApdfActionPerformed
    // TODO add your handling code here:
    try {/* w  ww .j  a v  a  2 s.  c  om*/
        Connection miConexion = (Connection) Conexion.GetConnection();
        Statement statement = (Statement) miConexion.createStatement();
        String folio = txtFolio.getText();
        ResultSet rs = statement
                .executeQuery("SELECT COUNT(*) AS num FROM tb_reserva WHERE num ='" + folio + "' ");
        int rows = 0;
        if (rs.next()) {
            rows = rs.getInt("num");
        }

        if (rows > 0) {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Documento Pdf", "pdf");
            chooser.setFileFilter(filter);
            chooser.setDialogTitle("Guardar archivo");
            chooser.setMultiSelectionEnabled(false);
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

                String file = chooser.getSelectedFile().toString().concat(".pdf");
                FileOutputStream archivo = new FileOutputStream(file);
                Document documento = new Document();
                PdfWriter.getInstance(documento, archivo);
                documento.open();

                documento.add(new Paragraph("DOCUMENTO RESERVA", new Font(Font.FontFamily.HELVETICA, 20)));
                documento.add(new Paragraph(""));
                documento.add(new Paragraph(""));
                documento.add(new Paragraph("Nmero de folio : " + folio));
                documento.add(new Paragraph(""));
                rs = statement.executeQuery("SELECT * FROM tb_reserva WHERE num ='" + folio + "' ");
                while (rs.next()) {
                    documento.add(new Paragraph("Pedido Para Dia : " + rs.getString("fecha") + " A Las : "
                            + rs.getString("hora") + "  Horas"));
                    documento.add(
                            new Paragraph("Devolucion De Pedido Para El Dia : " + rs.getString("fecha_entrega")
                                    + " A Las : " + rs.getString("hora_entrega") + "  Horas"));
                    documento.add(new Paragraph("Solicit : " + rs.getString("rut")));
                    documento.add(new Paragraph("Fue Atendido Por : " + rs.getString("cod_admin")));
                    documento.add(new Paragraph("Sera Instalado Por : " + rs.getString("cod_instalador")));

                    int estado = rs.getInt("estado");
                    switch (estado) {
                    case 1:
                        documento.add(new Paragraph("ESTADO : EMITIDO"));
                        break;
                    case 2:
                        documento.add(new Paragraph("ESTADO : ANULADO"));
                        break;
                    case 3:
                        documento.add(new Paragraph("ESTADO : EJECUTADO"));
                        break;
                    }

                    documento.add(new Paragraph("OBSERVACION : " + rs.getString("observacion"),
                            new Font(Font.FontFamily.HELVETICA, 14)));
                }

                PdfPTable table = new PdfPTable(3);

                rs = statement.executeQuery("SELECT * FROM tb_reserva_detalle WHERE folio ='" + folio + "' ");

                table.addCell("LINEA");
                table.addCell("CODIGO");
                table.addCell("DESCRIPCION");
                while (rs.next()) {
                    table.addCell(rs.getString("num"));
                    table.addCell(rs.getString("codigo"));
                    table.addCell(rs.getString("descripcion"));

                }

                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento
                        .add(new Paragraph(" Equipos Solicitados : ", new Font(Font.FontFamily.HELVETICA, 14)));
                documento.add(new Paragraph(" "));
                documento.add(table);

                documento.close();
            } // fin saveshowdialog                    
        } else {
            JOptionPane.showMessageDialog(this, "Numero de folio ingresado no se encuentra en sistema");
        }

    } // fin trycatch
    catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage());
    }

}

From source file:appArriendos.frmVistaInstalador.java

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
    // TODO add your handling code here:

    try {/* w w w. j  a v a 2s.c o m*/
        Connection miConexion = (Connection) Conexion.GetConnection();
        Statement statement = (Statement) miConexion.createStatement();
        String folio = txtFolio.getText();
        ResultSet rs = statement
                .executeQuery("SELECT COUNT(*) AS num FROM tb_reserva WHERE num ='" + folio + "' ");
        int rows = 0;
        if (rs.next()) {
            rows = rs.getInt("num");
        }

        if (rows > 0) {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Documento Pdf", "pdf");
            chooser.setFileFilter(filter);
            chooser.setDialogTitle("Guardar archivo");
            chooser.setMultiSelectionEnabled(false);
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

                String file = chooser.getSelectedFile().toString().concat(".pdf");
                FileOutputStream archivo = new FileOutputStream(file);
                Document documento = new Document();
                PdfWriter pdfw = PdfWriter.getInstance(documento, archivo);
                documento.open();

                PdfContentByte cb = pdfw.getDirectContent();

                Barcode39 codeEAN = new Barcode39();
                codeEAN.setCodeType(Barcode.UPCA);
                //                 codeEAN.setCode(folio);
                codeEAN.setCode(folio);
                documento.add(codeEAN.createImageWithBarcode(cb, null, null));
                documento.add(new Paragraph("DOCUMENTO INSTALACIN", new Font(Font.FontFamily.HELVETICA, 16)));
                documento.add(new Paragraph(" "));

                documento.add(new Paragraph(" "));
                rs = statement.executeQuery("SELECT * FROM tb_reserva WHERE num ='" + folio + "' ");

                while (rs.next()) {

                    documento.add(new Paragraph("Nmero de folio : " + folio));
                    documento.add(new Paragraph("Pedido Para Dia : " + rs.getString("fecha") + " A Las : "
                            + rs.getString("hora") + "  Horas"));
                    documento.add(
                            new Paragraph("Devolucion De Pedido Para El Dia : " + rs.getString("fecha_entrega")
                                    + " A Las : " + rs.getString("hora_entrega") + "  Horas"));
                    documento.add(new Paragraph("Solicit : " + rs.getString("rut")));
                    documento.add(new Paragraph("Fue Atendido Por : " + rs.getString("cod_admin")));
                    documento.add(new Paragraph("Sera Instalado Por : " + rs.getString("cod_instalador")));

                    int estado = rs.getInt("estado");
                    switch (estado) {
                    case 1:
                        documento.add(new Paragraph("ESTADO : EMITIDO"));
                        break;
                    case 2:
                        documento.add(new Paragraph("ESTADO : ANULADO"));
                        break;
                    case 3:
                        documento.add(new Paragraph("ESTADO : EJECUTADO"));
                        break;
                    }

                    documento.add(new Paragraph("OBSERVACION : " + rs.getString("observacion"),
                            new Font(Font.FontFamily.HELVETICA, 14)));
                }

                PdfPTable table = new PdfPTable(3);

                rs = statement.executeQuery("SELECT * FROM tb_reserva_detalle WHERE folio ='" + folio + "' ");

                table.addCell("LINEA");
                table.addCell("CODIGO");
                table.addCell("DESCRIPCION");
                while (rs.next()) {
                    table.addCell(rs.getString("num"));
                    table.addCell(rs.getString("codigo"));
                    table.addCell(rs.getString("descripcion"));

                }

                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento
                        .add(new Paragraph(" Equipos Solicitados : ", new Font(Font.FontFamily.HELVETICA, 14)));
                documento.add(new Paragraph(" "));
                documento.add(table);
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph("Informacin sobre los prestamos : "));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(
                        "Despues de la firma del presente comprobante, el Cliente se responsabiliza por los equipos entregados por el Instalador. ",
                        new Font(Font.FontFamily.HELVETICA, 8)));
                documento.add(new Paragraph(
                        "Es Responzabilidad del Instalador retirar los equipos en la fecha acordada.",
                        new Font(Font.FontFamily.HELVETICA, 8)));
                documento.add(new Paragraph(
                        "En caso de ocurrir robo, deterioro, o perdida de los equipos entregados. El Ministerio Publico iniciara los sumarios correspondeientes segun la gravedad del incidente.",
                        new Font(Font.FontFamily.HELVETICA, 8)));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph("Recib Conforme"));

                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph("Firma Cliente : _____________________________________"));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento.add(new Paragraph("Firma Instalador : __________________________________"));

                documento.close();
            } // fin saveshowdialog
        } else {
            JOptionPane.showMessageDialog(this, "Numero de folio ingresado no se encuentra en sistema");
        }

    } // fin trycatch
    catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage());
    }

}

From source file:appArriendos.frmVistaReserva.java

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
    // TODO add your handling code here:

    try {//from   w w  w. java  2 s.  c  o  m
        Connection miConexion = (Connection) Conexion.GetConnection();
        Statement statement = (Statement) miConexion.createStatement();
        String folio = txtFolio.getText();
        ResultSet rs = statement
                .executeQuery("SELECT COUNT(*) AS num FROM tb_reserva WHERE num ='" + folio + "' ");
        int rows = 0;
        if (rs.next()) {
            rows = rs.getInt("num");
        }

        if (rows > 0) {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Documento Pdf", "pdf");
            chooser.setFileFilter(filter);
            chooser.setDialogTitle("Guardar archivo");
            chooser.setMultiSelectionEnabled(false);
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

                String file = chooser.getSelectedFile().toString().concat(".pdf");
                FileOutputStream archivo = new FileOutputStream(file);
                Document documento = new Document();
                PdfWriter pdfw = PdfWriter.getInstance(documento, archivo);
                documento.open();
                PdfContentByte cb = pdfw.getDirectContent();

                Barcode39 codeEAN = new Barcode39();
                codeEAN.setCodeType(Barcode.UPCA);
                codeEAN.setCode(folio);
                documento.add(codeEAN.createImageWithBarcode(cb, null, null));
                documento.add(new Paragraph("DOCUMENTO RESERVA", new Font(Font.FontFamily.HELVETICA, 16)));

                documento.add(new Paragraph(""));

                documento.add(new Paragraph("Nmero de folio : " + folio));
                documento.add(new Paragraph(""));
                rs = statement.executeQuery("SELECT * FROM tb_reserva WHERE num ='" + folio + "' ");
                while (rs.next()) {
                    documento.add(new Paragraph("Nmero de folio : " + folio));
                    documento.add(new Paragraph("Pedido Para Dia : " + rs.getString("fecha") + " A Las : "
                            + rs.getString("hora") + "  Horas"));
                    documento.add(
                            new Paragraph("Devolucion De Pedido Para El Dia : " + rs.getString("fecha_entrega")
                                    + " A Las : " + rs.getString("hora_entrega") + "  Horas"));
                    documento.add(new Paragraph("Solicit : " + rs.getString("rut")));
                    documento.add(new Paragraph("Fue Atendido Por : " + rs.getString("cod_admin")));
                    documento.add(new Paragraph("Sera Instalado Por : " + rs.getString("cod_instalador")));

                    int estado = rs.getInt("estado");
                    switch (estado) {
                    case 1:
                        documento.add(new Paragraph("EMITIDO"));
                        break;
                    case 2:
                        documento.add(new Paragraph("ANULADO"));
                        break;
                    case 3:
                        documento.add(new Paragraph("EJECUTADO"));
                        break;
                    }
                }

                PdfPTable table = new PdfPTable(3);

                rs = statement.executeQuery("SELECT * FROM tb_reserva_detalle WHERE folio ='" + folio + "' ");

                table.addCell("LINEA");
                table.addCell("CODIGO");
                table.addCell("DESCRIPCION");
                while (rs.next()) {
                    table.addCell(rs.getString("num"));
                    table.addCell(rs.getString("codigo"));
                    table.addCell(rs.getString("descripcion"));

                }

                documento.add(new Paragraph(" "));
                documento.add(new Paragraph(" "));
                documento.add(table);

                documento.close();
            } // fin saveshowdialog                    
        } else {
            JOptionPane.showMessageDialog(this, "Numero de folio ingresado no se encuentra en sistema");
        }

    } // fin trycatch
    catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage());
    }

}