RowSpan does not work in iTextSharp?

Please take a look at the following screen shot:

enter image description here

To the left, you see an HTML file rendered in a browser. To the right, you see that HTML file rendered to PDF using iText (the Java version). Note that the functionality of iTextSharp regarding HTML to PDF is identical to Java, hence you shouldn’t post questions saying “does not work in iTextSharp” because that sounds as if iTextSharp can’t achieve what you want to do (which is an incorrect allegation), whereas the actual problem is caused by some individual errors you made when writing your code. It is not friendly to blame a tool for your own errors 😉

There are three reasons why your application doesn’t work:

  1. Your HTML doesn’t make sense. I had to clean it up (change <br> into <br />, introduce the correct CSS, correct the column-count for some rows,…) and make it XHTML before it rendered correctly in a browser. You can find the HTML that was used in the screenshot here: table2_css.html
  2. You are using HTMLWorker instead of XML Worker, and you are right: HTMLWorker has no support for CSS. Saying CSS doesn’t work in iTextSharp is wrong. It doesn’t work when you use HTMLWorker, but that’s documented: the CSS you need works in XML Worker.
  3. You are probably using an old version of iTextSharp, and you are right: CSS and table support wasn’t as good as in older versions of iTextSharp when compared to the most recent version.

See the XML Worker page on the official iText site for more info. Apart from iTextSharp, you also need to download XML Worker. The examples are written in Java, but you should have no problem converting them to C#. The example I used to make the PDF in the screen shot (html_table_4.pdf) can be found here: ParseHtmlTable4

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5
    document.close();
}

Leave a Comment