Asp.net mvc 3 Web UI和itextsharp中的阿拉伯文字体
我找不到我的MVC3网站正确显示阿拉伯语字体而我的pdf不正确的原因 我在我的网站上使用bliss字体Asp.net mvc 3 Web UI和itextsharp中的阿拉伯文字体,asp.net-mvc-3,pdf,fonts,itextsharp,font-face,Asp.net Mvc 3,Pdf,Fonts,Itextsharp,Font Face,我找不到我的MVC3网站正确显示阿拉伯语字体而我的pdf不正确的原因 我在我的网站上使用bliss字体 @font-face { font-family: 'blissregular'; src: url('/Fonts/blissregular-webfont.eot'); src: url('/Fonts/blissregular-webfont.eot?#iefix') format('embedded-opentype'), url('/Fonts/blissregular-w
@font-face {
font-family: 'blissregular';
src: url('/Fonts/blissregular-webfont.eot');
src: url('/Fonts/blissregular-webfont.eot?#iefix') format('embedded-opentype'),
url('/Fonts/blissregular-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;}
一切正常。
之后,我想创建输出的pdf,但阿拉伯语字体不会出现
我在谷歌上搜索过,知道字体必须有阿拉伯字符才能正确显示。我已改为arial字体(包含阿拉伯字符)和。。。pdf成功了
所以。。。用bliss字体(没有阿拉伯字符)我怎么可能在网站上看到阿拉伯字体
我真的很困惑
谢谢大家 对于浏览器遇到的每个字符,它都会在当前字体中查找匹配的字形。如果字体没有该字形,它将查找任何备用字体,以查看它们是否有该字形。最终,每个浏览器都有一组核心的默认字体,这是最终的退路。当您指定字体Bliss但使用阿拉伯语字符时,您可能只是看到浏览器的备用字体 PDF不是这样工作的。如果你说某物正在使用字体
XYZ
,那么它将尝试使用该字体渲染它,否则将失败
最简单的方法可能是在CSS中添加支持这些字符的字体
.myclass{font-family: blissregular, Arial}
如果这不起作用,您可能需要手动注入字体。(事实上,我也不是100%确定iText支持@font-face。)iText有一个助手类,可以帮你解决这个问题,但不幸的是,C#link已经不起作用了。这很简单,您只需创建FontSelector
类的一个实例,按照查找字符的顺序调用AddFont
,然后将一个字符串传递给Process()
方法,该方法返回一个可以添加的短语。下面是展示这一点的基本示例代码。我为我的示例文本道歉,我是英语母语,所以我只是搜索了一些可以使用的东西,我希望我没有弄坏它或使它倒退
在处理HTML时,您需要跳过几个额外的步骤,但希望您能够解决这个问题
//Sample string. I apologize, this is from a Google search so I hope it isn't backward
var testString = "يوم الاثنين \"monday\" in Arabic";
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Standard PDF setup
using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//This is a font that I know *does not* support Arabic characters, substitute with your own font if you don't have it
var gishaFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "gisha.ttf");
var gishaBaseFont = BaseFont.CreateFont(gishaFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var gishaFont = new iTextSharp.text.Font(gishaBaseFont, 20);
//Add our test string using just a normal font, this *will not* display the Arabic characters
doc.Add(new Phrase(testString, gishaFont));
//This is a font that I know *does* support Arabic characters
var arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
var arialBaseFont = BaseFont.CreateFont(arialFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var arialFont = new iTextSharp.text.Font(arialBaseFont, 20);
//Create our font selector specifying our most specific font first
var Sel = new FontSelector();
Sel.AddFont(gishaFont);
Sel.AddFont(arialFont);
//Have the font selector process our text into a series of chunks wrapped in a phrase
var newPhrase = Sel.Process(testString);
//Add the phrase, this will display both characters
doc.Add(newPhrase);
//Clean up
doc.Close();
}
}
}
所以Bliss没有阿拉伯字符,但你的网站仍在显示它们?你的PDF根本没有显示它们?没错。如果我的英语不清楚,我很抱歉。我以前遇到过这个问题,你可以看看我的问题“使用itextsharp将阿拉伯语“unicode”内容html或xml转换为PDF”,这可能会帮到你