How to create a PDF using iText for Android

Asked

Viewed 4,265 times

1

I was trying to create a pdf file on my Android application, as I could not find anything that worked, today I managed to solve the problem on my own. Follow the code to whom you are interested.

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 for implementing more advanced features such as, inserting borders and lines. It’s not a question, just post anything relevant, just to aggregate and help anyone who’s trying the same.

2 answers

1


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

pdf

  • Thanks friend, it is very useful yes. Here is a link with some basic functions to those who need to create a table. http://itextpdf.com/themes/keyword.php?id=152

0

Does anyone know how to create a PDF that mirrors a Holerite (Paycheck)?

I was imagining two options: The first is creating an XML that represents the entire layout of the paycheck, so it would facilitate me in the insertion of the fields and the organization.

The second is through an image, and would place the information according to the alignment margins.

In my point of view the first would be the most viable, someone could give me a North to go on?

  • Yuri, welcome to Sopt, before we begin a look at our Tour The place where you posted your question is for answers only, your message could be a comment on the question, however you still don’t have enough reputation for it. I suggest you ask your own question explaining your problem, but first read How to create a Minimum, Complete and Verifiable example

Browser other questions tagged

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