Error creating PDF file on Android

4

I have a problem creating a PDF using iText on Android. I have to save in the internal memory of the device, but it is giving error because I do not know the way to save because in the tutorial it saved in C:/temp .

This is the code

public class PdfActivity extends Activity {

    private static String file = "texto";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf);

        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
            addTitlePage(document);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addTitlePage(Document document) 
        throws DocumentException{
            Paragraph preface = new Paragraph();

            //pula uma linha
            addEmptyFile(preface, 1);
            //titulo com font grande
            preface.add(new Paragraph("Documento de Teste", catFont));

            addEmptyFile(preface, 1);
            preface.add(new Paragraph("Conteudo teste do corpo, um texto simples para ser exibido como corpo do documento", smallBold));
            document.add(preface);
            document.newPage();

    }
    //metodo para pular uma linha
    private static void addEmptyFile(Paragraph paragraph, int number) {
        for(int i=0; i<number; i++){
            paragraph.add(new Paragraph(""));
        }
    }           
}

The variable file is where the path goes. An image of LogCat:

    
asked by anonymous 21.05.2014 / 04:31

1 answer

3

In which way are you saving the PDF file? in any case you may be looking at Variáveis de Ambiente for information on system directories. See an example, a FooBar.pdf file will be created in the default documents directory.

 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdf);

    try 
    {
       Document document=new Document();
       File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); 
       File pdffile = new File(path, "FooBar.pdf");
       PdfWriter.getInstance(document, new FileOutputStream(pdffile));
       document.open();
       document.add(new Paragraph("Foo Bar Bar Baz Baz Foo"));
       document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
21.05.2014 / 14:07