[英]Apply custom font to html in itextsharp
我有代碼
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);
}
我的問題是無法將字體應用於代碼
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));
我已經能夠使用以下代碼在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 style屬性添加字體名稱:
YOURDIVNAME.Attributes.Add("style", "font-family: brush script mt italic;");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.