'How to create a Paragraph with iTextPDF?

I have this mobile app where I need to export some rows of the database to pdf. I need to have a pdf structure in order to get the information correctly placed. This is the code I have but for some reason the PDF cames blank. The main objective is to have a title at the beginning, then a row from the database, then a "enter" and show again another row from the database query result.

Document doc = new Document();
String outPath = Environment.getExternalStorageDirectory() + "/"+nomePDF+".pdf";

try {
    PdfWriter.getInstance(doc, new FileOutputStream(outPath));
    Font f=new Font(Font.FontFamily.TIMES_ROMAN,30.0f,Font.UNDERLINE, BaseColor.RED);
    Paragraph p = new Paragraph("Group/Machine",f);
    p.setAlignment(Element.ALIGN_CENTER);
    p.setSpacingAfter(40f);
    doc.add(p);
    bd = new Databse(MainActivity.this);

    Cursor c = bd.getGroupsNamesMachines();
    if (c.moveToFirst()) {
        do {
            String group = c.getString(c.getColumnIndex("groupname"))+"\n"+c.getString(c.getColumnIndex("otherrow"));
            doc.add(new Paragraph(group));
            doc.add(new Paragraph("\n"));

        } while (c.moveToNext());
    }

doc.open();
doc.close();

The structure I am aiming for is:

TITLE

row1
row2

row2
row2


Solution 1:[1]

You try to add content to the Document doc before opening it!

Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(outPath));
[...]
doc.add(p);
[...]
        doc.add(new Paragraph(group));
        doc.add(new Paragraph("\n")); 
[...]
doc.open();
doc.close();

Move the doc.open() call up to a position between the PdfWriter.getInstance call and the first doc.add call.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkl