Jero Ternero Scarpa
Jero Ternero Scarpa

Reputation: 3

Itext , pdf created couldn't be open

i wanted to create pdf with Itext , everything works until i've made the runnable jar . Adobe reader shows that message : "Adobe Reader could not open xxx.pdf because it is either not a supported file type or because the file has been damaged (for example. it was sent as an email attachment and wasn't correctly decoded)." And here is my code :

    String ruta = txtruta.getText();
    Document doc = new Document();

    try {
        FileOutputStream archivo = new FileOutputStream(ruta + ".pdf"); //crear archivo con su ruta

        doc.open();

        PdfPTable tabla = new PdfPTable(8); //creacion de una tabla de 8 columnas

        tabla.addCell("Celda 1");// addCell() agrega una celda a la tabla, el cambio de fila ocurre automaticamente al llenar la fila
        tabla.addCell("Celda 2");
        tabla.addCell("Celda 3");
        tabla.addCell("Celda 4");
        tabla.addCell("Celda 5");
        tabla.addCell("Celda 6");
        tabla.addCell("Celda 7");
        tabla.addCell("Celda 8");// aca se completa una fila

        doc.add(tabla);

        doc.close();

        JOptionPane.showMessageDialog(null, "PDF creado correctamente");

    } catch (Exception e) {
        System.out.println("Error: "+ e);
    }

Upvotes: 0

Views: 2035

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

There's a line missing in your code:

String ruta = txtruta.getText();
Document doc = new Document();

try {
    FileOutputStream archivo = new FileOutputStream(ruta + ".pdf"); //crear archivo con su ruta
    PdfWriter.getInstance(document, archivo);
    doc.open();

    PdfPTable tabla = new PdfPTable(8); //creacion de una tabla de 8 columnas

    tabla.addCell("Celda 1");// addCell() agrega una celda a la tabla, el cambio de fila ocurre automaticamente al llenar la fila
    tabla.addCell("Celda 2");
    tabla.addCell("Celda 3");
    tabla.addCell("Celda 4");
    tabla.addCell("Celda 5");
    tabla.addCell("Celda 6");
    tabla.addCell("Celda 7");
    tabla.addCell("Celda 8");// aca se completa una fila

    doc.add(tabla);

    doc.close();

    JOptionPane.showMessageDialog(null, "PDF creado correctamente");

} catch (Exception e) {
    System.out.println("Error: "+ e);
}

Do you see?

Without PdfWriter.getInstance(document, archivo); you're creating a FileOutputStream, but nothing is ever written to that stream. It's a file with 0 bytes.

Upvotes: 1

Related Questions