简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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