gpt4 book ai didi

c# - 如何在我的网站中使用 PDFsharp 中的字体?

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:27 25 4
gpt4 key购买 nike

我只是在 Azure WebApplication 网站上使用 Arial 字体,但是当我到达这一行时:

MainFont = new XFont("Arial", FontSize);

它抛出异常读取:无法检索字体数据。

我原以为 Arial 会安装在服务器上……我也尝试将其更改为 Sans-Serif 以匹配 Microsoft 生成的网站的默认字体……但它仍然失败。

我也尝试过将 Arial.ttf 添加到项目中,但没有成功。

最佳答案

感谢@PDFSharp 团队的指点。这是我对 PdfSharp 1.5 beta3b 的实现:

将你想要的字体添加到你的项目中 - 在我下面的示例中,我将 Arial 放在 MyProject\fonts\arial\arial.ttf 等中。将每个字体文件设置为嵌入式资源(属性 -> 构建操作)。

像这样使用静态调用只应用一次字体解析器:

MyFontResolver.Apply(); // Ensures it's only applied once

这是字体解析器类:

class MyFontResolver : IFontResolver
{
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
// Ignore case of font names.
var name = familyName.ToLower().TrimEnd('#');

// Deal with the fonts we know.
switch (name)
{
case "arial":
if (isBold)
{
if (isItalic)
return new FontResolverInfo("Arial#bi");
return new FontResolverInfo("Arial#b");
}
if (isItalic)
return new FontResolverInfo("Arial#i");
return new FontResolverInfo("Arial#");
}

// We pass all other font requests to the default handler.
// When running on a web server without sufficient permission, you can return a default font at this stage.
return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
}

/// <summary>
/// Return the font data for the fonts.
/// </summary>
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "Arial#":
return FontHelper.Arial;

case "Arial#b":
return FontHelper.ArialBold;

case "Arial#i":
return FontHelper.ArialItalic;

case "Arial#bi":
return FontHelper.ArialBoldItalic;
}

return null;
}


internal static MyFontResolver OurGlobalFontResolver = null;

/// <summary>
/// Ensure the font resolver is only applied once (or an exception is thrown)
/// </summary>
internal static void Apply()
{
if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null)
{
if (OurGlobalFontResolver == null)
OurGlobalFontResolver = new MyFontResolver();

GlobalFontSettings.FontResolver = OurGlobalFontResolver;
}
}
}


/// <summary>
/// Helper class that reads font data from embedded resources.
/// </summary>
public static class FontHelper
{
public static byte[] Arial
{
get { return LoadFontData("MyProject.fonts.arial.arial.ttf"); }
}

public static byte[] ArialBold
{
get { return LoadFontData("MyProject.fonts.arial.arialbd.ttf"); }
}

public static byte[] ArialItalic
{
get { return LoadFontData("MyProject.fonts.arial.ariali.ttf"); }
}

public static byte[] ArialBoldItalic
{
get { return LoadFontData("MyProject.fonts.arial.arialbi.ttf"); }
}

/// <summary>
/// Returns the specified font from an embedded resource.
/// </summary>
static byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();

// Test code to find the names of embedded fonts
//var ourResources = assembly.GetManifestResourceNames();

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;
}
}
}

这是一个单一的、完整的、工作类,基于这两个几乎相同的帖子:this blogthis forum .

关于c# - 如何在我的网站中使用 PDFsharp 中的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37395555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com