Apply custom font to html in itextsharp

和自甴很熟 提交于 2019-12-24 00:33:43

问题


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


回答1:


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



回答2:


I have been able to implement custom fonts via HTML in iTextSharp using the following code:

Step 1: Copy your font files to a subdirectory of your website, for my purposes I used "/media/fonts"

Step 2: Register the directory using the code below:

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:

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:

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


来源:https://stackoverflow.com/questions/18145985/apply-custom-font-to-html-in-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!