Example usage for com.itextpdf.text Document close

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

Introduction

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

Prototype

boolean close

To view the source code for com.itextpdf.text Document close.

Click Source Link

Document

Has the document already been closed?

Usage

From source file:by.bsuir.group172301.matskevich.tour.util.PDFCreator.java

public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName, int col1, int col2,
        double perc) {
    PdfWriter writer = null;//w w w. j a v  a2 s  .  c om

    Document document = new Document();

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        Paragraph paragraph = new Paragraph();
        // We add one empty line

        paragraph = new Paragraph("Results:");
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);
        paragraph = new Paragraph("Test before: " + col1);
        document.add(paragraph);
        paragraph = new Paragraph("Test after: " + col2);
        document.add(paragraph);
        paragraph = new Paragraph("Percentage: " + perc);
        document.add(paragraph);
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 150, 250);

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

From source file:ca.sqlpower.wabit.report.LayoutToPDF.java

License:Open Source License

public void writePDF() throws DocumentException, FileNotFoundException, PrinterException {
    monitorableHelper.setStarted(true);/*www.j  a  v a 2 s .  c o  m*/
    int pageNum = 0;

    int numPages = layout.getNumberOfPages();
    monitorableHelper.setJobSize(numPages);
    Page page = layout.getPage();
    OutputStream out = fileOS;
    Rectangle pageSize;
    pageSize = new Rectangle(page.getWidth(), page.getHeight());

    Document pdfDoc = new Document(pageSize, 0f, 0f, 0f, 0f);

    PdfWriter pdfOut = PdfWriter.getInstance(pdfDoc, out);
    pdfDoc.open();
    pdfDoc.addCreator("Wabit " + WabitVersion.VERSION);
    PdfContentByte pdfContent = pdfOut.getDirectContent();
    Graphics2D pdfGraphics = null;
    try {
        while (pageNum < numPages) {
            monitorableHelper.checkCancelled();
            monitorableHelper.setProgress(pageNum);
            pdfGraphics = pdfContent.createGraphics(pageSize.getWidth(), pageSize.getHeight());
            int flag = layout.print(pdfGraphics, layout.getPageFormat(pageNum), pageNum);

            if (watermarker != null) {
                java.awt.Rectangle watermarkSize = new java.awt.Rectangle();
                watermarkSize.setSize(Math.round(pageSize.getWidth()), Math.round(pageSize.getHeight()));
                watermarker.watermark(pdfGraphics, watermarkSize);
            }

            pdfGraphics.dispose();
            pdfGraphics = null;

            if (flag == Printable.NO_SUCH_PAGE)
                break;

            pdfDoc.newPage();

            pageNum++;
        }
    } finally {
        if (pdfGraphics != null)
            pdfGraphics.dispose();
        if (pdfDoc != null)
            pdfDoc.close();
        monitorableHelper.setFinished(true);
    }
}

From source file:ca.uwo.csd.cs2212.team02.MainWindow.java

/**
 * Export current page to PDF/*w  w  w  . j  a  v  a 2 s .c o  m*/
 *
 * @param jpanel Panel to be exported to PDF
 */
public void createPDF(JLayeredPane jpanel) {
    playButtonSound();
    com.itextpdf.text.Rectangle r = new com.itextpdf.text.Rectangle(0, 0, jpanel.getWidth(),
            jpanel.getHeight());
    Document doc = new Document(r);
    String filename = fileChooser();
    if (filename != "INVALID") {
        try {
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filename));
            doc.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(PageSize.A4.getHeight() * 2, PageSize.A4.getWidth() * 2);
            Graphics2D g2;
            g2 = tp.createGraphics(jpanel.getWidth(), jpanel.getHeight(), new DefaultFontMapper());
            jpanel.print(g2);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
            PopUpWindow saveSuccess = new PopUpWindow(filename, "Save Successful!", 8);
        } catch (Exception e) {
            new PopUpWindow("Please try again.", "Error saving file");
        }
    }
    doc.close();
}

From source file:CadastroDeMusicas.Principal.java

public static void main(String[] args) throws Exception {
    ConecBanco c = new ConecBanco(con);
    Genero g;/*  w  ww . j  a  v a  2  s. c om*/
    Artista a1;
    Album a2;
    Musica m;
    String nome, tipo;
    int nota, duracao, ano;
    int opc;
    try {
        new Principal();
    } catch (ClassNotFoundException | SQLException e) {
        System.out.println("No conectado.");
    }

    do {
        opc = Integer.parseInt(JOptionPane.showInputDialog(
                "Informe a OPO:\n1.Inserir\n2.Deletar\n3.Pesquisar\n4.Relatorio em Pdf\n5.Sair"));
        switch (opc) {
        case 1:
            nome = JOptionPane.showInputDialog("Genero: ");
            g = new Genero(nome);
            nome = JOptionPane.showInputDialog("Artista: ");
            a1 = new Artista(nome);
            nome = JOptionPane.showInputDialog("Album: ");
            ano = Integer.parseInt(JOptionPane.showInputDialog("Ano: "));
            a2 = new Album(nome, ano);
            nome = JOptionPane.showInputDialog("Musica: ");
            nota = Integer.parseInt(JOptionPane.showInputDialog("Nota (1/10): "));
            duracao = Integer.parseInt(JOptionPane.showInputDialog("Durao: "));
            m = new Musica(nome, nota, duracao);
            c.cadastrar(g, a1, a2, m);
            break;
        case 2:
            System.out.println("DELETAR MUSICA: ");
            nome = JOptionPane.showInputDialog("Musica: ");
            c.deletar(nome);
            break;
        case 3:
            System.out.println("PESQUISAR MUSICA: ");
            ResultSet rs = ConecBanco.pesquisar();
            if (rs != null) {
                try {
                    while (rs.next()) {
                        System.out.println("MUSICA:  " + rs.getString("nome"));
                        System.out.println("NOTA:  " + rs.getString("nota"));
                        System.out.println("DURAO:  " + rs.getString("duracao"));
                        System.out.println("ALBUM:  " + rs.getString("nomeAL"));
                        System.out.println("ANO:  " + rs.getString("ano"));
                        System.out.println("ARTISTA:  " + rs.getString("nomeAR"));
                        System.out.println("GENERO:  " + rs.getString("nomeGE"));

                    }
                } catch (SQLException e) {
                    System.out.println("Problema para exibir registros!");
                }
            } else {
                System.out.println("A pesquisa no retornou nenhum registro!");
            }
            break;
        case 4:
            Document doc = null;
            OutputStream os = null;
            String add;
            System.out.println("LISTA DE MUSICAS: ");
            try {
                doc = new Document(PageSize.A4, -1, -1, 25, 1) {
                };
                os = new FileOutputStream("Relatorio.pdf");
                PdfWriter.getInstance((com.itextpdf.text.Document) doc, os);
                doc.open();
                Image img = Image.getInstance("images.jpg");
                img.setAlignment(Element.ALIGN_CENTER);
                doc.add(img);
                PdfPTable TB1, TB2, TB3, TB4;
                TB1 = new PdfPTable(2);
                TB2 = new PdfPTable(3);
                TB3 = new PdfPTable(4);
                TB4 = new PdfPTable(5);
                PdfPCell T1, T2, T3, T4, T5;
                T1 = new PdfPCell(new Paragraph("\nCADASTRO DE MUSICAS\n"));
                T1.setColspan(2);
                TB1.addCell(T1);
                T2 = new PdfPCell(new Paragraph("\nGENEROS\n"));
                T2.setColspan(2);
                TB1.addCell(T2);
                ResultSet res, res1, res2, res3;
                res = ConecBanco.relatorioGenero();
                if (res != null) {
                    try {
                        while (res.next()) {
                            TB1.addCell("ID:  " + res.getString("id"));
                            TB1.addCell("NOME:  " + res.getString("nomeGE"));

                        }
                        doc.add(TB1);
                    } catch (SQLException e) {
                        System.out.println("Problema para exibir registros!");
                    }
                } else {
                    System.out.println("A pesquisa no retornou nenhum registro!");
                }
                T3 = new PdfPCell(new Paragraph("\nARTISTAS\n"));
                T3.setColspan(3);
                TB2.addCell(T3);
                res1 = ConecBanco.relatorioArtista();
                if (res1 != null) {
                    try {
                        while (res1.next()) {
                            TB2.addCell("ID:  " + res1.getString("id"));
                            TB2.addCell("NOME:  " + res1.getString("nomeAR"));
                            TB2.addCell("GENERO ID:  " + res1.getString("genero_id"));
                        }
                        doc.add(TB2);
                    } catch (SQLException e) {
                        System.out.println("Problema para exibir registros!");
                    }
                } else {
                    System.out.println("A pesquisa no retornou nenhum registro!");
                }
                T4 = new PdfPCell(new Paragraph("\nALBUM\n"));
                T4.setColspan(4);
                TB3.addCell(T4);
                res2 = ConecBanco.relatorioAlbum();
                if (res2 != null) {
                    try {
                        while (res2.next()) {
                            TB3.addCell("ID:  " + res2.getString("id"));
                            TB3.addCell("NOME:  " + res2.getString("nomeAL"));
                            TB3.addCell("ANO:  " + res2.getString("ano"));
                            TB3.addCell("ARTISTA ID:  " + res2.getString("artista_id"));
                        }
                        doc.add(TB3);
                    } catch (SQLException e) {
                        System.out.println("Problema para exibir registros!");
                    }
                } else {
                    System.out.println("A pesquisa no retornou nenhum registro!");
                }
                T5 = new PdfPCell(new Paragraph("\nMUSICA\n"));
                T5.setColspan(5);
                TB4.addCell(T5);
                res3 = ConecBanco.relatorioMusica();
                if (res3 != null) {
                    try {
                        while (res3.next()) {
                            TB4.addCell("ID:  " + res3.getString("id"));
                            TB4.addCell("NOME:  " + res3.getString("nome"));
                            TB4.addCell("NOTA:  " + res3.getString("nota"));
                            TB4.addCell("DURAO:  " + res3.getString("duracao"));
                            TB4.addCell("ALBUM ID:  " + res3.getString("album_id"));
                        }
                        doc.add(TB4);
                    } catch (SQLException e) {
                        System.out.println("Problema para exibir registros!");
                    }
                } else {
                    System.out.println("A pesquisa no retornou nenhum registro!");
                }
            } finally {
                if (doc != null) {
                    doc.close();
                }
                if (os != null) {
                    os.close();
                }
            }
            break;
        case 5:
            System.out.println("SAINDO...");
            break;
        default:
            System.out.println("OPO INVALIDA!");
        }
    } while (opc != 5);

}

From source file:Capa_Modelo.Reportes.java

public void generarReporteInventario() {
    Connection con;/*from   w  ww  . jav a 2 s .  co m*/
    ResultSet res;
    Statement sentencia;
    Document documento = new Document(PageSize.A4);
    con = ConexionDB.GetConnection();
    try {
        sentencia = con.createStatement();
        res = sentencia.executeQuery(
                "SELECT Cantidad, Cantidadmin,  Cantidadmax, Medicamento.Nombre, Medicamento.FechaElaboracion, Medicamento.Composicion, Medicamento.FechaVencimiento, Medicamento.Laboratorio FROM Inventario inner join Medicamento on Inventario.ID_Medicamento = Medicamento.ID_Medicamento ");
        PdfWriter.getInstance(documento, new FileOutputStream(
                "reportes/reporte_de_inventario " + fechaActual() + " " + horaActual() + ".pdf"));
        documento.open();
        float[] columnWidths = { 2, 2, 2, 2, 2, 2, 2, 2 };
        PdfPTable table = new PdfPTable(columnWidths);
        table.setWidthPercentage(100);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        Font f = new Font(Font.FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
        PdfPCell cell = new PdfPCell(new Phrase("Reporte de Medicamentos", f));
        cell.setBackgroundColor(GrayColor.GRAYBLACK);
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setColspan(8);
        table.addCell(cell);
        table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table.addCell("Medicamento");
            table.addCell("Composicion");
            table.addCell("Laboratorio");
            table.addCell("Fecha de Elaboracion");
            table.addCell("Fecha de Vencimiento");
            table.addCell("Cantidad minima");
            table.addCell("Cantidad actual");
            table.addCell("Cantidad maxima");
        }
        table.setHeaderRows(3);
        table.setFooterRows(1);
        table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        while (res.next()) {
            table.addCell(res.getString("Nombre"));
            table.addCell(res.getString("Composicion"));
            table.addCell(res.getString("Laboratorio"));
            table.addCell(res.getString("FechaElaboracion"));
            table.addCell(res.getString("FechaVencimiento"));
            table.addCell(res.getString("Cantidadmin"));
            table.addCell(res.getString("Cantidad"));
            table.addCell(res.getString("Cantidadmax"));
        }
        documento.add(table);
        documento.close();
        JOptionPane.showMessageDialog(null, "Reporte de Inventario generado correctamente");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error al conectar con la base de datos");
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar la ruta del archivo");
    } catch (DocumentException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar el archivo");
    }

}

From source file:Capa_Modelo.Reportes.java

public void generarReporteVencimiento() {
    Connection con;/* w ww .j  a  v a  2  s . c  o m*/
    ResultSet res;
    Statement sentencia;
    Document documento = new Document(PageSize.A4);
    con = ConexionDB.GetConnection();
    try {
        sentencia = con.createStatement();
        res = sentencia.executeQuery(
                "SELECT Cantidad, Cantidadmin, Cantidadmax, Medicamento.Nombre, Medicamento.FechaElaboracion, Medicamento.FechaVencimiento, Medicamento.Laboratorio FROM Inventario inner join Medicamento on Inventario.ID_Medicamento = Medicamento.ID_Medicamento ");
        PdfWriter.getInstance(documento, new FileOutputStream(
                "reportes/reporte_de_vencimiento " + fechaActual() + " " + horaActual() + ".pdf"));
        documento.open();
        float[] columnWidths = { 2, 2, 2, 2 };
        PdfPTable table = new PdfPTable(columnWidths);
        table.setWidthPercentage(100);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        Font f = new Font(Font.FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
        PdfPCell cell = new PdfPCell(new Phrase("Reporte de Vencimiento", f));
        cell.setBackgroundColor(GrayColor.GRAYBLACK);
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setColspan(4);
        table.addCell(cell);
        table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table.addCell("Medicamento");
            table.addCell("Laboratorio");
            table.addCell("Fecha de Vencimiento");
            table.addCell("Cantidad actual");
        }
        table.setHeaderRows(3);
        table.setFooterRows(1);
        table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        while (res.next()) {
            if (vencido(res.getDate("FechaVencimiento"))) {
                table.addCell(res.getString("Nombre"));
                table.addCell(res.getString("Laboratorio"));
                table.addCell(res.getString("FechaVencimiento"));
                table.addCell(res.getString("Cantidad"));
            }
        }
        documento.add(table);
        documento.close();
        JOptionPane.showMessageDialog(null, "Reporte de Vencimiento generado correctamente");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error en conectar con base de datos");
    } catch (DocumentException ex) {
        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, "El documento no se pudo generar");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, "EL archivo no se abri");
    }
}

From source file:Capa_Modelo.Reportes.java

public void generarReporteConsumoMedicamentos() {
    DecimalFormat df = new DecimalFormat("####0.00");
    Connection con;/*from   w  w w. java 2 s . c  o  m*/
    ResultSet res;
    Statement sentencia;
    Document documento = new Document(PageSize.A4);
    double consumo, cantidad;
    double porcentaje;
    consumo = cantidad = 0;
    con = ConexionDB.GetConnection();
    try {
        sentencia = con.createStatement();
        res = sentencia.executeQuery(
                "SELECT Medicamento.Nombre, Medicamento.Composicion, Medicamento.Laboratorio, Inventario.Cantidad, SUM(MedicinaPaciente.Cantidad) as Consumo FROM MedicinaPaciente INNER JOIN Medicamento ON MedicinaPaciente.ID_Medicamento = Medicamento.ID_Medicamento INNER JOIN Inventario ON MedicinaPaciente.ID_Medicamento = Inventario.ID_Medicamento  GROUP BY Medicamento.Nombre, Medicamento.Composicion , Medicamento.Laboratorio, Inventario.Cantidad ORDER BY Consumo");
        PdfWriter.getInstance(documento, new FileOutputStream(
                "reportes/reporte_de_consumo_medicamentos " + fechaActual() + " " + horaActual() + ".pdf"));
        documento.open();
        float[] columnWidths = { 2, 2, 2, 2, 2, 2 };
        PdfPTable table = new PdfPTable(columnWidths);
        table.setWidthPercentage(100);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        Font f = new Font(Font.FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
        PdfPCell cell = new PdfPCell(new Phrase("Reporte de Consumo Medicamentos", f));
        cell.setBackgroundColor(GrayColor.GRAYBLACK);
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setColspan(8);
        table.addCell(cell);
        table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table.addCell("Medicamento");
            table.addCell("Composicion");
            table.addCell("Laboratorio");
            table.addCell("Cantidad");
            table.addCell("Unidades consumidas");
            table.addCell("Porcentaje de consumo");
        }
        table.setHeaderRows(3);
        table.setFooterRows(1);
        table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        while (res.next()) {
            consumo = res.getInt("Consumo");
            cantidad = res.getInt("Cantidad");
            porcentaje = consumo / cantidad * 100;
            System.out.println("consumo: " + consumo + " cantidad: " + cantidad + " porcentaje: " + porcentaje);
            table.addCell(res.getString("Nombre"));
            table.addCell(res.getString("Composicion"));
            table.addCell(res.getString("Laboratorio"));
            table.addCell(res.getString("Cantidad"));
            table.addCell(res.getString("Consumo"));
            table.addCell(((df.format(porcentaje))) + "%");

        }
        documento.add(table);
        documento.close();
        JOptionPane.showMessageDialog(null, "Reporte de Consumo de Medicamentos generado correctamente");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error al conectar con la base de datos");
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar la ruta del archivo");
    } catch (DocumentException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar el archivo");
    }
}

From source file:Capa_Modelo.Reportes.java

public void generarReporteESMedicamentos() {
    Connection con;/*from w  ww.  ja  va 2s .co  m*/
    ResultSet res, res2;
    Statement sentencia;
    Document documento = new Document(PageSize.A4);
    con = ConexionDB.GetConnection();
    try {
        sentencia = con.createStatement();
        res = sentencia.executeQuery(
                "SELECT Medicamento.Nombre, Medicamento.Composicion, Medicamento.Laboratorio, Medicamento.FechaLlegada, Inventario.Cantidad FROM Medicamento INNER JOIN Inventario ON Medicamento.ID_Medicamento = Inventario.ID_Medicamento ORDER BY Inventario.Cantidad, Medicamento.Nombre desc");
        PdfWriter.getInstance(documento, new FileOutputStream("reportes/reporte_de_entrada-salida_medicamentos "
                + fechaActual() + " " + horaActual() + ".pdf"));
        documento.open();
        float[] columnWidths = { 2, 2, 2, 2, 2 };
        PdfPTable table = new PdfPTable(columnWidths);
        table.setWidthPercentage(100);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        Font f = new Font(Font.FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
        PdfPCell cell = new PdfPCell(new Phrase("Reporte de Entrada de Medicamentos", f));
        cell.setBackgroundColor(GrayColor.GRAYBLACK);
        cell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell.setColspan(8);
        table.addCell(cell);
        table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table.addCell("Medicamento");
            table.addCell("Composicion");
            table.addCell("Laboratorio");
            table.addCell("Fecha de Entrada");
            table.addCell("Cantidad");
        }
        table.setHeaderRows(3);
        table.setFooterRows(1);
        table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        while (res.next()) {
            table.addCell(res.getString("Nombre"));
            table.addCell(res.getString("Composicion"));
            table.addCell(res.getString("Laboratorio"));
            table.addCell(res.getString("FechaLlegada"));
            table.addCell(res.getString("Cantidad"));
        }
        documento.add(table);
        documento.newPage();
        res2 = sentencia.executeQuery(
                "SELECT Medicamento.Nombre, Medicamento.Composicion, Medicamento.Laboratorio, MedicinaPaciente.FechaEntrega, SUM(MedicinaPaciente.Cantidad) AS Entregados FROM Medicamento INNER JOIN MedicinaPaciente ON Medicamento.ID_Medicamento = MedicinaPaciente.ID_Medicamento  group by Medicamento.Nombre, Medicamento.Composicion, Medicamento.Laboratorio,MedicinaPaciente.FechaEntrega ORDER BY Entregados");
        float[] columnWidths2 = { 2, 2, 2, 2, 2 };
        PdfPTable table2 = new PdfPTable(columnWidths);
        table2.setWidthPercentage(100);
        table2.getDefaultCell().setUseAscender(true);
        table2.getDefaultCell().setUseDescender(true);
        PdfPCell cell2 = new PdfPCell(new Phrase("Reporte de Salida de Medicamentos", f));
        cell2.setBackgroundColor(GrayColor.GRAYBLACK);
        cell2.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        cell2.setColspan(8);
        table2.addCell(cell2);
        table2.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
        for (int i = 0; i < 2; i++) {
            table2.addCell("Medicamento");
            table2.addCell("Composicion");
            table2.addCell("Laboratorio");
            table2.addCell("Fecha de Salida");
            table2.addCell("Cantidad");
        }
        table2.setHeaderRows(3);
        table2.setFooterRows(1);
        table2.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
        table2.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        while (res2.next()) {
            table2.addCell(res2.getString("Nombre"));
            table2.addCell(res2.getString("Composicion"));
            table2.addCell(res2.getString("Laboratorio"));
            table2.addCell(res2.getString("FechaEntrega"));
            table2.addCell(res2.getString("Entregados"));
        }
        documento.add(table2);
        documento.close();
        JOptionPane.showMessageDialog(null,
                "Reporte de Entrada y Salida de Medicamentos generado correctamente");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error al conectar con la base de datos");
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar la ruta del archivo");
    } catch (DocumentException ex) {
        JOptionPane.showMessageDialog(null, "Error al generar el archivo");
    }
}

From source file:casariego.jorge.createpdf.Pdf1Activity.java

public static void createPDF() {
    //create document object
    Document document = new Document();

    //output file path
    String outpath = Environment.getExternalStorageDirectory() + "/theBestPdf/PDF1.pdf";

    try {/*www.  j  a  v a 2s . c  o m*/
        PdfWriter.getInstance(document, new FileOutputStream(outpath));
        document.open();
        addMetaData(document);
        addTitlePage(document);
        addContent(document);
        document.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:cashiermode.PrintRecieptPdf.java

public static void printPdf() {
    Document transPdf = null;
    PdfWriter writer = null;/*from  w  ww. j a  v  a 2 s.co m*/

    // NumberFormat fmt = NumberFormat.getPercentInstance();
    // DecimalFormat fmt1 = new DecimalFormat("0.0");
    Font font1 = new Font(Font.FontFamily.HELVETICA, 3, Font.NORMAL);
    Font font2 = new Font(Font.FontFamily.TIMES_ROMAN, 7);
    Font font3 = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    Font font4 = new Font(Font.FontFamily.COURIER, 9, Font.BOLD);

    try {
        transPdf = new Document();
        File file = new File("Report.pdf");
        System.out.println("File crearted");

        writer = PdfWriter.getInstance(transPdf, new FileOutputStream(file));
        transPdf.open();
        transPdf.setMargins(0, 5, 0, 2);
    } catch (FileNotFoundException | DocumentException ex) {

    }

    try {
        transPdf.add(new Paragraph(
                "                                             TERM TWO ACCADEMIC REPORT FORM", font3));
        transPdf.add(new Paragraph(
                "NAME :                                    ADM NO:             FORM             ", font3));
        transPdf.add(
                new Paragraph("FORM POS         OUT OF           CLASS POS           OUT OF         ", font3));

        PdfPTable table = new PdfPTable(10); // 4 column
        table.setWidthPercentage(100);
        table.setSpacingBefore(10f);
        table.setSpacingAfter(10f);

        PdfPCell cell0 = new PdfPCell(new Paragraph("SUBJECT", font2));
        PdfPCell cell1 = new PdfPCell(new Paragraph("CAT1", font2));
        PdfPCell cell2 = new PdfPCell(new Paragraph("CAT2", font2));
        PdfPCell cell3 = new PdfPCell(new Paragraph("EXAM", font2));
        PdfPCell cell4 = new PdfPCell(new Paragraph("TOTAL", font2));
        PdfPCell cell5 = new PdfPCell(new Paragraph("GRADE", font2));
        PdfPCell cell6 = new PdfPCell(new Paragraph("PNTS", font2));
        PdfPCell cell7 = new PdfPCell(new Paragraph("POS", font2));
        PdfPCell cell8 = new PdfPCell(new Paragraph("RMK", font2));
        PdfPCell cell9 = new PdfPCell(new Paragraph("SIGN", font2));

        // cell2.setColspan(2);
        table.addCell(cell0);
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
        table.addCell(cell7);
        table.addCell(cell8);
        table.addCell(cell9);

        for (int i = 0; i < 11; i++) {

            //                cell0 = new PdfPCell(new Paragraph(mysubs[i], font2));
            cell1 = new PdfPCell(new Paragraph("", font2));
            cell2 = new PdfPCell(new Paragraph("", font2));
            cell3 = new PdfPCell(new Paragraph("", font2));
            cell4 = new PdfPCell(new Paragraph("", font2));
            cell5 = new PdfPCell(new Paragraph("", font2));
            cell6 = new PdfPCell(new Paragraph("", font2));
            cell7 = new PdfPCell(new Paragraph("", font2));
            cell8 = new PdfPCell(new Paragraph("", font2));
            cell9 = new PdfPCell(new Paragraph("", font2));

            table.addCell(cell0);
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);
            table.addCell(cell5);
            table.addCell(cell6);
            table.addCell(cell7);
            table.addCell(cell8);
            table.addCell(cell9);

        }

        transPdf.add(table);

        Paragraph p = new Paragraph(
                "Closing Date                                  Next Term Begins On                          ",
                font3);
        p.setSpacingBefore(185f);
        transPdf.add(p);
        transPdf.add(new Paragraph(
                "Outstanding Fee                    Next Term Fee                     Total                 ",
                font3));
        transPdf.add(new Paragraph(
                "Feedback                                                Sign             Date              ",
                font3));
        transPdf.add(new Paragraph("                       Parent/Guardian", font3));
        Paragraph lastp = new Paragraph("Closing date is on   27/14/2005", font3);
        lastp.setSpacingAfter(0f);
        transPdf.add(lastp);

        //transPdf.newPage();

    } catch (DocumentException ex) {
        System.err.println("NOT YET.." + ex.getMessage());
    }

    transPdf.close();

}