mercredi 28 octobre 2015

Error Creating a PDF using iText

I am relatively new to Java and Android. Having said that I am using the iText.jar file in my app to create a PDF from data entered by the user. I am starting with the basic example just to see how it works and play with it. For some reason, I either get errors, either it is read only or it does not exist. Hoping someone has run into a similar problem with a fix. My code is below that I am using. I have added the permissions in the android manifest. I didn't include every bit of code, just the relevant stuff. The GlobalVars is a class I use to store all the public variables.Also, the toast statement I am using to help debug and is where I am getting these error messages......

<public static final File location =     Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);>

<try {
        createPdf();
    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
        errorString = e.getMessage();
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }>

<public void createPdf() throws FileNotFoundException, DocumentException {
    FileOutputStream pdfFile = new FileOutputStream(location.getAbsolutePath() + "/temp.pdf");
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, pdfFile);
    // step 3
    document.open();
    // step 4
    document.add(createDataTable());
    // step 5
    document.close();
}

public static PdfPTable createDataTable() {
    // a table with two columns
    PdfPTable table = new PdfPTable(4);
    // the cell object
    PdfPCell cell;
    // we add the cells with table.addCell()

    for (int x = 0; x < 40; x++) {
        cell = new PdfPCell(Phrase.getInstance(x + ": " + GlobalVars.inspectionResults[x]));
        table.addCell(cell);
    }
    return table;
}>

Aucun commentaire:

Enregistrer un commentaire