How to create a PDF using iText for Android

1

I was trying to create a pdf file in my Android application, as I did not find anything that worked, today I was able to solve the problem on my own. Follow the code to whoever interests.

Here are the imports corresponding to iText.jar

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

Here the code working perfectly

private void criandoPdf() {

    try {

         String filename = "teste.pdf";

        document = new Document(PageSize.A4);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/MeuPdf";

        File dir = new File(path, filename);
        if (!dir.exists()) {
            dir.getParentFile().mkdirs();
        }

        FileOutputStream fOut = new FileOutputStream(dir);
        fOut.flush();

        PdfWriter.getInstance(document, fOut);
        document.open();
        document.add(new Paragraph("Aqui esta meu pdf"));


    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        document.close();
    }

} 

If someone knows how to work well with iText, feel free to post a good link to implement more advanced features such as insert borders and lines. It's not a question, just post anything relevant, just to add and help those who are trying the same.

    
asked by anonymous 28.10.2015 / 02:58

1 answer

1

See the gist I created below, it might still be useful for you.

pdf

    
28.10.2015 / 13:35