tags:

views:

54

answers:

1

Hi. I m creating a table using iText. Each table has 2 columns and have no borders except for left most, right most, top most and bottom most side of the table. I am able to achieve this but the problem occurs when new page begins. I want the to draw a horizontal line to the table at the end of page and another horizontal line when it begins. I have tried using

@Override
public void onEndPage(PdfWriter arg0, Document arg1) {
    PdfPCell pdfpcells[] = pdfptable.getRow(pdfptable.getRows().size()-1).getCells();
    pdfpcells[0].setBorderWidthBottom(0.5f);
    if(pdfpcells[1] != null){ //There is a possibility that there are odd number of elements
       pdfpcells[1].setBorderWidthBottom(0.5f);
    }
}

for drawing horizontal line at the end of page assuming this function is called every time page ends and hence uses current number of rows. pdfptable is declared as class variable. This doesn't seem to work. I am using latest version of iText. Thanks.

A: 

Can you post the code that constructs the table? Do you make one per page or are you relying on the auto-split of the PdfPTable?

The code below should do the trick:

 PdfPCell pdfPCells[] = table.getRow(table.getRows().size() - 1).getCells();
 for (PdfPCell pdfPCell : pdfPCells) {
     pdfPCell.setBorder(PdfPCell.BOTTOM);
 }

As you can see there is no need for you to worry about the number of elements in the array, if you just use a for-each loop.

Jes