2

I want to generate a PDF with 3 columns by dynamic data. I have tried with my this code,

public class Clazz {

public static final String RESULT = "result.pdf";
private    String[] data = {"1","2","3","4","5","6","7","8"};

private void go() throws Exception {

    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
    doc.open();

    PdfPTable mainTable = new PdfPTable(3);
    PdfPCell cell;

    for (int i = 0; i < data.length; i+=2) {
        cell = new PdfPCell(new Phrase(data[i]));
        PdfPTable table = new PdfPTable(1);
        table.addCell(cell);
        if (i+1 <= data.length -1) {
           cell = new PdfPCell(new Phrase(data[i + 1]));
           table.addCell(cell);
        } else {
            cell = new PdfPCell(new Phrase(""));
            table.addCell(cell);
        }
        mainTable.addCell(table);
    }

    doc.add(mainTable);
    doc.close();

}
}

I want to print like this in my PDF:

In first page it will print 1 2 3 and then in next page it will print 4 5 6 and then in next page it will print 7 8 and blank cell.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Somnath Dutta
  • 87
  • 1
  • 8

2 Answers2

0

In order to go to the next page you have to use the newPage() method from the Document class. You can see an example of newPage here.

Here is a possible solution. It can surely be improved, it is just something that does what you were asking:

private static void go() throws Exception {
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
    doc.open();

    PdfPTable mainTable = new PdfPTable(3);
    PdfPCell cell;

    //went to <= data.length to cover the case of empty cell after 7,8
    //a better approach could be more suitable here
    for (int i = 0; i <= data.length; i++) {
        PdfPTable table = new PdfPTable(1);
        if (i < data.length) {
           cell = new PdfPCell(new Phrase(data[i]));
           table.addCell(cell);
        } else {
            cell = new PdfPCell(new Phrase(""));
            table.addCell(cell);
        }

        //go to next page after completing each row
        if(i != 0 && i%3 == 0)
        {
          doc.add(mainTable);
          doc.newPage();
          mainTable =  new PdfPTable(3);
        }

        mainTable.addCell(table);
    }

    doc.add(mainTable);
    doc.close();
}
Dragos Geornoiu
  • 527
  • 2
  • 8
  • 17
0

From a purely academic perspective , here is the code. Is it optimized , no. I have just taken 3 things a) Need a table with 3 columns b) Needs a page break after each row c) Blank cells need to be populated ""

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class CreateTable {

    /** The resulting PDF file. */
    public static final String RESULT = "first_table.pdf";

    /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new CreateTable().createPdf(RESULT);
    }

    /**
     * Creates a PDF with information about the movies
     * 
     * @param filename
     *            the name of the PDF file that will be created.
     * @throws DocumentException
     * @throws IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        createCustomTables(document);
        // step 5
        document.close();
    }

    public static void createCustomTables(Document document) throws DocumentException {
        String[] data = { "1", "2", "3", "4", "5", "6", "7", "8" };
        PdfPTable table = null;
        int arrayLength = data.length;
        int mod3 = data.length % 3;
        int iterCounter = (mod3 == 0) ? arrayLength : arrayLength + (3 - mod3);

        System.out.println(iterCounter);

        for (int i = 0; i <= iterCounter; i++) {
            String string = (arrayLength > i) ? data[i] : "";
            if (i % 3 == 0) {
                table = new PdfPTable(3);
                document.newPage();
            }
            PdfPCell cell = new PdfPCell(new Phrase(string));
            table.addCell(cell);
            document.add(table);
        }
        return;
    }
}
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24