user2061099
user2061099

Reputation:

How to fill a PdfPTable column by column instead of row by row, using iText

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

Upvotes: 7

Views: 12226

Answers (3)

Ajay Jape
Ajay Jape

Reputation: 1

        String[] dataArray = new String[] { "1", "2", "3", "4", "5", "6" };
        PdfPTable pdfPTable = new PdfPTable(3);

        PdfPCell pdfPCell = new PdfPCell();
        Integer dataArrayIndex = 0, counter = 0;

        for (int size = 0; size < dataArray.length; size++) {

            pdfPCell = new PdfPCell(new Phrase(dataArray[dataArrayIndex]));
            pdfPTable.addCell(pdfPCell);

            dataArrayIndex += dataArray.length / pdfPTable.getNumberOfColumns();

            if ((size + 1) % pdfPTable.getNumberOfColumns() == 0)
                dataArrayIndex = ++counter;
        }

Upvotes: 0

Balkrushna
Balkrushna

Reputation: 1

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;
}

Upvotes: 0

John
John

Reputation: 349

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

    }
}

Upvotes: 6

Related Questions