itextsharp - CSS not getting applied - C# .NET

I'm using iTextSharp for converting a HTML page to PDF. I'm making use of the helper class given here and I've also tried to make use of StyleSheet.LoadTagStyle() to apply CSS. But nothing seems to work. Any insights?

EDIT

I'm able to add styles like this -

.mystyle
{
   color: red;
   width: 400px;
}

With the following code -

StyleSheet css = new StyleSheet();
css.LoadStyle("mystyle", "color", "red");
css.LoadStyle("mystyle", "width", "400px");

But what happens when I’ve complex styles like this?

div .myclass
{
    /*some styles*/
}

td a.hover
{
    /*some styles*/
}

td .myclass2
{
    /*some styles*/
}    
.myclass .myinnerclass
{
    /*some styles*/
}

How to add it using iTextSharp?


Solution 1:

you're on the right track with using StyleSheet.LoadTagStyle().

basically it's a four step process:

  1. get the HTML in a string
  2. instantiate a StyleSheet object and call StyleSheet.LoadTagStyle() for each style you want.
  3. call HTMLWorker.ParseToList()
  4. add the IElement(s) returned from above call to the Document object.

here's a simple HTTP handler:

<%@ WebHandler Language='C#' Class='styles' %>
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;  
using iTextSharp.text.pdf;  

public class styles : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    HttpResponse Response = context.Response;
    Response.ContentType = "application/pdf";
    string Html = @"
<h1>h1</h1>
<p>A paragraph</p>    
<ul> 
<li>one</li>   
<li>two</li>   
<li>three</li>   
</ul>";
    StyleSheet styles = new StyleSheet();
    styles.LoadTagStyle(HtmlTags.H1, HtmlTags.FONTSIZE, "16");
    styles.LoadTagStyle(HtmlTags.P, HtmlTags.FONTSIZE, "10");
    styles.LoadTagStyle(HtmlTags.P, HtmlTags.COLOR, "#ff0000");
    styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, "10");
    styles.LoadTagStyle(HtmlTags.LI, HtmlTags.LEADING, "16");
    using (Document document = new Document()) {
      PdfWriter.GetInstance(document, Response.OutputStream);
      document.Open();
      List<IElement> objects = HTMLWorker.ParseToList(
        new StringReader(Html), styles
      );
      foreach (IElement element in objects) {
        document.Add(element);
      }
    }
 }
  public bool IsReusable {
      get { return false; }
  }
} 

you need version 5.0.6 to run the code above. support for parsing HTML has been greatly improved.

if you want to see what tags are supported by the current version, see the SVN for the HtmlTags class.

Solution 2:

var reader = new StringReader(text);
var styles = new StyleSheet();
styles.LoadTagStyle("body", "face", "Arial");
styles.LoadTagStyle("body", "size", fontSize + "px");
styles.LoadTagStyle("body", "font-weight", "bold");

ArrayList list = HTMLWorker.ParseToList(reader, styles);
for (int k = 0; k < list.Count; k++)
{

  var element = (IElement)list[k];

  if (element is Paragraph)
  {
    var paragraph = (Paragraph)element;
    paragraph.SpacingAfter = 10f;
    cell.AddElement(paragraph);
  }
 else
  cell.AddElement((IElement)list[k]);
}