C# – HTML to PDF – CSS in iTextSharp

ccssheighthtmlitextsharp

I am trying to build a pdf file from html code using iTextSharp.

Here is a sample of the HTML:

<div style="height:400px; font-family:Courier; font-size:9px">
    herrow
</div>
<span style="padding-left:100px;">
    world
</span>

Running as an html file it works fine putting 400px between hello and world and 100px before world.

Here is the code I am using to convert the html to pdf:

    List<IElement> htmlList = HTMLWorker.ParseToList(new StringReader(htmlText), new StyleSheet());

    foreach (IElement element in htmlList)
    {
        doc.Add(element);
    }

It does apply the font-family and font-size in the resulting pdf.
However, it ignores the div height and the span width in the pdf that is produced.

Is it possible to use heights / widths / padding etc., when converting html to pdf with iTextSharp?

If not is there a work around, or another free .dll that I can use to do this?

Best Answer

Just pull out height attribute outside the style tag and use it like this:-

<div height='400' style="font-family:Courier;font-size:9px">

herrow

</div>