C# Azure PDF Sharp未使用Unicode字体
我们有一个C#网站,使用Azure云服务托管,并使用PDF Sharp生成PDF文档 我们使用的是Arial Unicode MS常规字体,因为我们可以显示日文字体。当网站在本地运行(从Windows 7上的Visual Studio)时,字体将正确呈现 我们使用Azure的启动脚本将字体安装到云服务服务器上,因为默认情况下没有安装字体。我已验证该字体已安装在云服务服务器(Windows Server 2012)上 在Azure托管的网站中,日文字体显示为正方形,尽管PDF属性确实表明使用的字体是Arial Unicode MS Regular 你知道为什么云服务服务器上没有正确使用字体吗C# Azure PDF Sharp未使用Unicode字体,c#,azure,pdf,unicode,fonts,C#,Azure,Pdf,Unicode,Fonts,我们有一个C#网站,使用Azure云服务托管,并使用PDF Sharp生成PDF文档 我们使用的是Arial Unicode MS常规字体,因为我们可以显示日文字体。当网站在本地运行(从Windows 7上的Visual Studio)时,字体将正确呈现 我们使用Azure的启动脚本将字体安装到云服务服务器上,因为默认情况下没有安装字体。我已验证该字体已安装在云服务服务器(Windows Server 2012)上 在Azure托管的网站中,日文字体显示为正方形,尽管PDF属性确实表明使用的字体
为了解决这个问题,我将问题交叉发布到PDFSharp论坛上,一个答案为我指明了正确的方向,创建了一个字体解析器:
PDFSharp的文档是不完整的,因为它声明GDI版本是用于.NET网站的正确版本,而实际上在本例中并非如此。相反,使用FontResolver的WPF构建工作正常
示例 在
Global.asax.cs
中设置FontResolver
:
PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();
创建一个名为MyFontResolver
的新类,该类使用嵌入资源中包含的其他字体系列扩展默认实现
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;
}
}
}
字体本身应使用build action=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;
}
}
从嵌入资源中读取字体数据的帮助器类
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;