Web UI和itextsharp中的阿拉伯字体

时间:2013-08-26 15:58:52

标签: asp.net-mvc-3 pdf fonts itextsharp font-face

我无法找到为什么我的MVC 3网站正确显示阿拉伯语字体而我的pdf没有显示原因。

我在网站上使用了一种幸福字体;

@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,但不会出现阿拉伯字体。

我已经用Google搜索并了解该字体必须具有正确显示的阿拉伯字符。我已经改为arial字体(包含阿拉伯字符)和... pdf工作。

那么......有可能用bliss字体(没有阿拉伯字符)我在网站上看到阿拉伯字体吗?

我真的很困惑....

非常感谢大家!

1 个答案:

答案 0 :(得分:0)

对于浏览器遇到的每个字符,它会在当前字体中查找匹配的字形。如果字体没有该字形,它会查找任何后备字体以查看它们是否具有该字形。最终,每个浏览器都有一组核心默认字体,这是最终的后备。当您指定字体Bliss但使用阿拉伯字符时,您可能只是看到浏览器的后备字体。

PDF不能那样工作。如果您说某些内容使用字体XYZ,那么它将尝试使用该字体呈现它或失败。

最简单的方法可能就是在CSS中添加支持这些字符的字体。

.myclass{font-family: blissregular, Arial}

如果这不起作用,您可能需要手动注入字体。 (实际上,我并不是100%肯定iText支持@ font-face。)iText有一个帮助程序类,可以为你找出Bruno talks about it here,但不幸的是C#链接不再起作用了。这很简单,您只需创建FontSelector类的实例,按照您希望查找字符的顺序调用AddFont,然后将字符串传递给Process()方法它会吐出你可以添加的Phrase。以下是显示此功能的基本示例代码。我为我的示例文本道歉,我是英国人,所以我只是搜索了一些东西,我希望我没有破坏它或者让它倒退。

在处理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();
        }
    }
}