Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...