I am using iTextsharp (version 4.1.6 - an LGPL fork) to generate a PDF that contains simply a table. My project is a .net 4 application.
The table can have lots of columns and lots of rows and the PDF will not contain anything else (so I want to use landscape layout on an A4 page).
I, therefore, knocked up this (I cut it down to the code that interacts with iTextSharp, you can guess what the columns
, data
, val
and ms
variables are):
// Create a landscape A4 page...
var doc = new Document(new Rectangle(297, 210), 10, 10, 10, 10);
PdfWriter.GetInstance(doc, ms);
doc.Open();
var table = new PdfPTable(columns.Count()) {HeaderRows = 1};
var titleFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10);
var bodyFont = FontFactory.GetFont(BaseFont.COURIER, 10);
// Add the column headings...
foreach (var c in columns)
{
var cell = new PdfPCell(new Phrase(c.Title, titleFont))
{
NoWrap = true
};
table.AddCell(cell);
}
// Add the data...
foreach (var o in data)
{
foreach (var c in columns)
{
var cell = new PdfPCell(new Phrase(val, bodyFont))
{
NoWrap = true
};
table.AddCell(cell);
}
}
doc.Add(table);
doc.Close();
Lovely except that creates a 1 page document where all the cells are overlapping and is completely unreadable.
What I want to achieve is something that looks like this:
So the columns never overlap, they just expand out onto the next page and the column headings are repeated as the rows extend down.
I've tried fiddling with KeepTogether
etc but I am flummoxed as to how to achieve this result.