itextsharp-CSS未得到应用-C#.NET

itextsharp-CSS未得到应用-C#.NET,第1张

itextsharp-CSS未得到应用-C#.NET

使用StyleSheet.LoadTagStyle()使您处在正确的轨道上。

基本上,这是一个四步过程:

  1. 以字符串形式获取HTML
  2. 实例化一个StyleSheet对象,然后为所需的每种样式调用 StyleSheet.LoadTagStyle()
  3. 调用HTMLWorker.ParseToList()
  4. 将上述调用返回的IElement添加到document对象。

这是一个简单的HTTP处理程序:

<%@ 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; }  }}

您需要版本5.0.6才能运行上面的代码。对解析HTML的支持已大大改善。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5440610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存