itextsharp ARIALUNI.TTF copy on hosting server

那年仲夏 提交于 2020-01-16 19:17:29

问题


My application is MVC4 c#, I am using itextsharp to generate PDF files. To print special characters ≥, I use:

string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
var bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
var f = new Font(bf, 10, Font.NORMAL);

When I published the application on a shared hosting server, got this error:

C:\Windows\Fonts\ARIALUNI.TTF not found as file or resource.

Copied the file to Content directory and tried to use:

string ARIALUNI_TFF1 = System.Web.HttpContext.Current.Server.MapPath("~/Content/ARIALUNI.TFF");
// FontFactory.Register(ARIALUNI_TFF1);
var bf1 = BaseFont.CreateFont(ARIALUNI_TFF1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var f1 = new Font(bf1, 10, Font.NORMAL);

I get the following error:

ARIALUNI.TFF' with 'Identity-H' is not recognized.

Would appreciate your suggestions.


回答1:


This really is strange issue I faced . Posting my solution. I needed to register this font to FontFactory and retrieve this font from factory using GetFont

Try this

    string path = System.Web.HttpContext.Current.Server.MapPath("~/Content/ArialUni.TFF");
    iTextSharp.text.Font fnt = new iTextSharp.text.Font();
    FontFactory.Register(path, "CustomAriel");
    fnt = FontFactory.GetFont("CustomAriel", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10,Font.NORMAL);

Hope this helps




回答2:


I replaced:

string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/ARIALUNI.TFF");I replaced:
    var bf = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    var f = new Font(bf, 10, Font.NORMAL);

with

string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
BaseFont bf = BaseFont.CreateFont(fontpath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 10, Font.NORMAL);

It worked.



来源:https://stackoverflow.com/questions/22728381/itextsharp-arialuni-ttf-copy-on-hosting-server

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