简体   繁体   English

将自定义字体应用于itextsharp中的html

[英]Apply custom font to html in itextsharp

I have code 我有代码

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);
    }

my issue is could not apply font to code 我的问题是无法将字体应用于代码

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));

I have been able to implement custom fonts via HTML in iTextSharp using the following code: 我已经能够使用以下代码在iTextSharp中通过HTML实现自定义字体:

Step 1: Copy your font files to a subdirectory of your website, for my purposes I used "/media/fonts" 第1步:出于我的目的,将字体文件复制到网站的子目录中,我使用了“ / media / fonts”

Step 2: Register the directory using the code below: 步骤2:使用以下代码注册目录:

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

Step 3: Loop through all of the fonts in the FontFactory object to get the font's name: 步骤3:遍历FontFactory对象中的所有字体以获取字体名称:

StringBuilder sb = new StringBuilder();
        foreach (string fontname in FontFactory.RegisteredFonts)
        {

            sb.Append(fontname + "\n");

        }

Step 4: Add the font name via a font-family style attribute: 步骤4:通过font-family style属性添加字体名称:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM