我们有一个使用Azure云服务托管的C#网站,并使用PDF Sharp生成PDF文档。
我们使用Arial Unicode MS Regular字体,因为我们要显示日文字体。当本地运行网站时(从Windows 7上的Visual Studio),字体可以正确呈现。
我们使用Azure的启动脚本将字体安装到Cloud Service服务器上,因为默认情况下未安装该字体。我已验证该字体已安装在Cloud Service服务器上(Windows Server 2012)。
在Azure托管网站中,日语字体显示为正方形,即使PDF属性确实指示使用的字体是Arial Unicode MS Regular。
为什么没有在Cloud Service服务器上正确使用该字体的任何想法?
答案 0 :(得分:4)
为了解决这个问题,我将问题交叉发布到了PDFSharp论坛Post,并且回答指出了我在创建字体解析器时使用的正确方向:
我将此部署到Azure云服务并确认正确使用了unicode字体。
PDFSharp的文档不完整,因为它声明GDI构建是用于.NET网站的正确构建,实际上它不是在这种情况下。相反,使用FontResolver的WPF构建工作。
示例强>
在FontResolver
中设置Global.asax.cs
:
PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();
创建一个名为MyFontResolver
的新类,该类使用嵌入式资源中包含的手动附加字体系列扩展默认实现。
应使用构建操作= Embedded Resource
将字体本身添加到字体目录中。
public class MyFontResolver : IFontResolver
{
public FontResolverInfo ResolveTypeface(string familyName,
bool isBold,
bool isItalic)
{
// Ignore case of font names.
var name = familyName.ToLower();
// Add fonts here
switch (name)
{
case "arial unicode ms":
return new FontResolverInfo("ArialUnicodeMS#");
}
//Return a default font if the font couldn't be found
//this is not a unicode font
return PlatformFontResolver.ResolveTypeface("Arial", isBold, isItalic);
}
// Return the font data for the fonts.
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "ArialUnicodeMS#": return FontHelper.ArialUnicodeMS; break;
}
return null;
}
}
Helper类,用于从嵌入式资源中读取字体数据。
public static class FontHelper
{
public static byte[] ArialUnicodeMS
{
//the font is in the folder "/fonts" in the project
get { return LoadFontData("MyApp.fonts.ARIALUNI.TTF"); }
}
/// Returns the specified font from an embedded resource.
static byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
{
if (stream == null)
throw new ArgumentException("No resource with name " + name);
int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);
return data;
}
}
}
生成PDF时照常定义字体,例如:
var style = document.Styles["Normal"];
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 8;