6

I am using Java with iText in order to generate some PDFs. I need to put text in columns, so I am trying to use PdfPTable. I create it with:

myTable = new PdfPTable(n);

n being the number of columns. The problem is that PdfPTable fills the table row by row, that is, you first give the cell in column 1 of row 1, then column 2 of row 1, and so on, but I need to do it column by column, because that is how the data is being fed to me.

I would use a Table (which lets you specify the position) like in http://stderr.org/doc/libitext-java-doc/www/tutorial/ch05.html, but I get a "could not resolve to a type", and my Eclipse can't find the proper import.

Edit: in case my previous explanation was confusing, what I want is to fill the table in this order:

1  3  5
2  4  6

Instead of this:

1  2  3
4  5  6
  • you mean this code is not working? aTable.addCell("2.2", new Point(2,2)); EDIT: which version of itext are you using? – nowhere Feb 11 '13 at 12:28
  • No, it tells me `addCell(String) in the type PdfPTable is not applicable for the arguments (String, Point)` and suggests removing `Point`. I am using iText 5.3.5, freshly downloaded. All the .jars have been added. @CosLu –  Feb 11 '13 at 12:38
  • by checking here http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html#addCell%28com.itextpdf.text.Image%29 seems that the addcell function doesn't accept the point as parameter. Don't know about that example you're looking at. Can't you for example generate a html table and then convert it? it might be easier. – nowhere Feb 11 '13 at 12:51
  • 1
    The tutorial seems to have been created for an old iText version (2.x, software from the last decade). When iText went from 2.x to 5.x, much changed. – mkl Feb 11 '13 at 12:51
  • He's right. The tutorial imports com.lowagie which is the old version. – nowhere Feb 11 '13 at 12:56
  • So, there is no way to do it? Is there an alternative to display n-column text in iText? –  Feb 11 '13 at 13:12
  • Hi, I have exactly the same problem now. Would be awesome if somewone could provide an answer ! – Floran Gmehlin Feb 13 '13 at 14:30
  • True. The only thing I can recommend to you is to stuff the whole table data in memory in a custom structure, and then give it to iText row by row, like it expects. @FloranGmehlin –  Feb 14 '13 at 13:54

2 Answers2

6

Here is one way: Create a PdfPTable with the number of columns desired, in your case 3. For each iteration through your data create a PdfPTable with 1 column. Create 2 PdfPCell objects, one containing the data element you are currently on and the other containing the next value in your data. So now you have a PdfPTable with 1 column and two rows. Add this PdfPTable to the PdfPTable that has 3 columns. Continue that until you've printed all your data. Better explained with code:

public class Clazz {

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

    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();

    }
}
John
  • 349
  • 3
  • 5
0

One way could be creating inner table of column no = 1 for each main column and add that into a main table.

private static PdfPTable writeColumnWise(String[] data, int noOfColumns, int noOfRows) {

    PdfPTable table = new PdfPTable(noOfColumns);
    PdfPTable columnTable = new PdfPTable(1);
    columnTable.getDefaultCell().setBorderWidth(0.0f);
    columnTable.getDefaultCell().setPadding(0.0f);

    for(int i=0; i<data.length; i++){
        if( i != 0 && i % noOfRows == 0 ){
            // add columnTable into main table
            table.addCell(columnTable);

            //re initialize columnTable for next column
            columnTable = new PdfPTable(1);
            columnTable.getDefaultCell().setBorderWidth(0.0f);
            columnTable.getDefaultCell().setPadding(0.0f);
        }

        PdfPCell cell = new PdfPCell(new Paragraph(data[i]));
        columnTable.addCell(cell);
    }

    // add columnTable for last column into main table
    table.addCell(columnTable);

    return table;
}