itextsharp – CSS not getting applied – C# .NET

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.

Leave a Comment