1

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:

Desired Table layout

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.

kmp
  • 10,535
  • 11
  • 75
  • 125
  • You won't achieve that with `document.add(table);` You'll need `table.writeSelectedRows()` instead. Also: does your customer know that you're using a version of iText that is no longer supported from a place where nobody at iText has put it by somebody who may have introduced all kinds of stuff that isn't endorsed by the iText developers? – Bruno Lowagie Nov 20 '13 at 11:36
  • I updated your question, removing the link to the unofficial link as well as your remark about your reason for using an unsupported version of iText. I've added some more background info here: http://lowagie.com/why5 – Bruno Lowagie Nov 20 '13 at 11:45
  • @Bruno Lowagie Thanks very much for your comments pointing out the issues. I understand that it would be a better idea to buy the commercial license for iTextSharp now (as that would be the only way to get a nice new version that can be used in a commercial application and I will certainly float this idea with my boss - although if I cannot fathom out this pagination issue it is moot anyway. To be honest I just looked on nuget for it and only found the GPL and that fork for the LGPL version so thought my only option was that fork but now I see there is a third choice of buying it). – kmp Nov 20 '13 at 12:11
  • @Bruno Lowagie Please forgive me though but having said that I am a bit confused. My question is (now "was" since your edit) a specific question about a specific version of iTextSharp. I was careful to link to that version exactly in case the procedure is different and the person who forked has made adjustments. It is a question about how to do a specific thing in a specific version of the library, that is all and your edit has changed the intention of the question. – kmp Nov 20 '13 at 12:12
  • Have you tried `writeSelectedRows()`? See http://itextpdf.com/examples/iia.php?id=90 (or http://tinyurl.com/itextsharpIIA2C04 if you want the C# counterpart). – Bruno Lowagie Nov 20 '13 at 13:01
  • Also see this answer: http://stackoverflow.com/a/20049909/231316 – Chris Haas Nov 20 '13 at 15:51
  • Thanks guys - I will have a look at that method and your examples. – kmp Nov 22 '13 at 08:11

0 Answers0