Error while creating PDF file on Android

Asked

Viewed 1,383 times

4

I have a problem to create a PDF using iText on Android. I have to save in the device’s internal memory, but this giving error because I do not know the way to save because in the tutorial he 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 That’s where the road goes. A picture of Logcat:

erro

1 answer

3


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

@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();
        }
    }
  • I got what was happening was that he did not find the library classes even exporting the jar, so I copied the file and pasted it in the libs folder and it worked, his code helped me a lot created the document, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.