Herin
Herin

Reputation: 722

itextsharp leaving first page empty and printing table conent in following table

I have three part in my PDF generating document using iTextSharp library.

The Header and footer I am printing using OnEndPage() of PdfPageEventHelper class. The header and footer printing well in all pages.But problem in printing large table content from middle of the page.

Now, I am facing problem when I have more that 100 table rows content to print on middle of the page.

On first page (middle of the page) I am printing table with more than 100 rows, but in this case it leave the first page empty and start printing table content on second page. Here is my table that I am trying to print on first page

PdfPTable table = new PdfPTable(5);
table.HeaderRows = 1;

table.SplitLate = false;
table.SplitRows = false;
table.SetTotalWidth(new float[] { 100, 75, 75, 75, 75 });
table.LockedWidth = true;

for (int i = 0; i < 150; i++)
        {
            PdfPCell cell = new PdfPCell(new phrase());
            cell.Colspan = 5;
            table.AddCell(cell);
        }

I am using iTextSharp version: 5.4.5.0

Is there any configuration need to do, that will prevent page break?

Expected result. Image-1

But I am getting actual result in this format

Actual result format, Image-2

Upvotes: 1

Views: 2141

Answers (2)

jpfm
jpfm

Reputation: 11

Same thing happened to me using nested tables. I solved this problem using .SplitLate = false; in the first table :)

Upvotes: 0

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Allow me to comment on these two lines:

table.SplitLate = false;
table.SplitRows = false;

The first line tells iTextSharp what it should do if a row doesn't fit a page. If you set this value to false, rows will be split late. For instance: you have a big row that doesn't fit the current page. In that case, the row will be forwarded to the next page. If it still doesn't fit the page, then another decision needs to be made: should the row be split or not?

The second line gives iTextSharp the answer to that question. If the value of SplitRows is true, then the row will be split. If the value is false, the row will be dropped. Allow me to quote the official documentation (p115 of "iText in Action - Second Edition"):

Ths is a dangerous line, because now not one row will be split. Rows that are too high to fit on a page will be dropped from the table!

I have the feeling that you want all the rows to show up in the table, so I suggest that you change the two lines I mentioned to:

table.SplitLate = true;
table.SplitRows = true;

Actually: why don't you just remove those two lines? The default value of SplitLate and of SplitRows is true.

Upvotes: 2

Related Questions