【问题标题】:Apply custom font to html in itextsharp在 itextsharp 中将自定义字体应用于 html
【发布时间】:2013-08-11 08:15:31
【问题描述】:

我有代码

FontFactory.Register(Server.MapPath("includes/fonts/Rockwell-Light.ttf"));
    StyleSheet style = new StyleSheet();
    style.LoadTagStyle("div", "face", "customfont");
    style.LoadTagStyle("div","encoding",BaseFont.CP1250);


foreach (IElement element in HTMLWorker.ParseToList(new StringReader("<div>" + getProductDescription((this.Product.Description != null) ? this.Product.Description : "") + "</div>"), style))
    {
        productDescCell.AddElement(element);
    }

我的问题是无法将字体应用于代码

【问题讨论】:

    标签: c# html asp.net itextsharp


    【解决方案1】:

    我已经能够使用以下代码在 iTextSharp 中通过 HTML 实现自定义字体:

    第 1 步:将您的字体文件复制到您网站的子目录中,出于我的目的,我使用了“/media/fonts”

    第 2 步:使用以下代码注册目录:

    FontFactory.RegisterDirectory(C.Server.MapPath("/Media/Fonts"));
    

    第 3 步:遍历 FontFactory 对象中的所有字体以获取字体名称:

    StringBuilder sb = new StringBuilder();
            foreach (string fontname in FontFactory.RegisteredFonts)
            {
    
                sb.Append(fontname + "\n");
    
            }
    

    第 4 步: 通过 font-family 样式属性添加字体名称:

    YOURDIVNAME.Attributes.Add("style", "font-family: brush script mt italic;");
    

    【讨论】:

      【解决方案2】:
      BaseFont rockwellBold = BaseFont.CreateFont(Server.MapPath("includes/fonts/") + "ROCKB.TTF", BaseFont.CP1250, BaseFont.EMBEDDED);
      Font rock_11_bold_header = new Font(rockwellBold, 11, Font.NORMAL, new BaseColor(190, 36, 34));
      PdfPCell descHeadrCell = new PdfPCell();
      descHeadrCell.AddElement(new Phrase("Demo"), rock_11_bold_header));
      

      【讨论】:

      • 这个答案与如何将自定义字体应用于html的问题有什么关系?
      • Rockwell 是自定义字体,需要将此字体应用到 itextsharp 中的 html 以生成 pdf。希望这是足够好的解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2014-10-13
      • 2012-02-27
      • 1970-01-01
      • 2013-01-03
      相关资源
      最近更新 更多