Cryn
Cryn

Reputation: 1025

iText tables - how to keep multi row cells on one page?

I'm creating a table in iText that lists events for a date. If there are mutliple events for a date, I set the rowspan property of the date-cell to the number of events. I'm currently using a row for each event, since I want to display additional info for the event and want to keep everything aligned. Basicly my table can look like this:

 Date   | Event     | Details
--------+-----------+---------------
 date 1 | event 1   | details 1
--------+-----------+---------------
 date 2 | event 2 1 | more 
        |           | details 2 1
        +-----------+---------------
        | event 2 2 | details 2 2
--------+-----------+---------------

the cell containing date 2 has a rowspan of 2.

I add the table using ColumnText.go() in a loop.

If I write the table to the document and there is only enough space left for event 2 1, event 2 2 goes to the new page. How can I force a new page before adding date 2?

Using PdfPTable.setSplitLate(true) doesn't seem to affect multi row cells. Neither does setting the fixed height of the date 2 cell to the combined heights of the two rows.

Are there any other ways?

Upvotes: 4

Views: 5991

Answers (2)

Abi Chhetri
Abi Chhetri

Reputation: 1437

        Document document = new Document(PageSize.A4, 30, 30, 100, 150);
        document.SetPageSize(iTextSharp.text.PageSize.A4);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        writer.PageEvent = new ITextEvents();
        document.Open();
        iTextSharp.text.Font fntHead2 = new iTextSharp.text.Font(bfntHead, 11, 1, BaseColor.BLACK);
        Paragraph para = new Paragraph();
        Chunk glue = new Chunk(new VerticalPositionMark());
        Phrase ph1 = new Phrase();

        Paragraph main = new Paragraph();
        ph1.Add(new Chunk("Left Side", fntHead2)); 
        ph1.Add(glue); // Here I add special chunk to the same phrase.    
        ph1.Add(new Chunk("Right Side", fntHead2)); 
        para.Add(ph1);
        document.Add(para);

Upvotes: 0

Michael Hogenson
Michael Hogenson

Reputation: 1391

You could create a table for each row and use PdfPTable.setKeepTogether(true) which works in even iText 2.1.7, though I wouldn't recommend staying with that version. Here is an example, where outputFile is a File type variable where the PDF file is being created.

Document document = new Document(new Rectangle(620, 150));
PdfWriter.getInstance(document, new FileOutputStream(outputFile));

document.open();

PdfPTable headerRow = new PdfPTable(3);
headerRow.setKeepTogether(true);
headerRow.addCell("Date");
headerRow.addCell("Event");
headerRow.addCell("Details");

PdfPTable firstRow = new PdfPTable(3);
firstRow.setKeepTogether(true);
firstRow.addCell("date 1");
firstRow.addCell("event 2 1");
firstRow.addCell("more\ndetails 2 1");

PdfPTable secondRow = new PdfPTable(3);
secondRow.setKeepTogether(true);
PdfPCell cell = new PdfPCell(new Phrase("date 2"));
cell.setRowspan(2);
secondRow.addCell(cell);
secondRow.addCell("event 2 1");
secondRow.addCell("more\ndetails 2 1");
secondRow.addCell("event 2 2");
secondRow.addCell("details 2 2");

document.add(headerRow);
document.add(firstRow);
document.add(secondRow);

document.close();

Honestly though, I think the nested table is the better idea.

Upvotes: 2

Related Questions