Joh*_*ris 4 fonts private pdfsharp
我已经下载并安装了 PDFSharp 1.5,但在使用私有字体时遇到了问题。我在测试中创建了一个 pdf 创建器,并且效果很好。当我将它加载到 Azure 时,它给了我无法加载字体的错误。做了研究,发现他们没有任何加载的字体,所以我必须使用私有字体。我只能找到旧的1.3版本的例子,方法改成新的。有人可以向我展示一个使用新版本 PDFSharp 的简单示例吗?
谢谢约翰
这是用于 PdfSharp 1.5 beta3b。这是一个基于其他答案和其他问题的链接的完整且固定的示例 - 但对于 Arial。
将您想要的字体添加到您的项目中 - 在下面的示例中,我输入Arial了MyProject\fonts\arial\arial.ttf等设置每个字体文件作为嵌入资源(属性- >生成操作)。
使用静态调用应用字体解析器,如下所示:
MyFontResolver.Apply(); // Ensures it's only applied once
Run Code Online (Sandbox Code Playgroud)
这是字体解析器类:
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);
}
public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "Arial#":
return LoadFontData("MyProject.fonts.arial.arial.ttf");;
case "Arial#b":
return LoadFontData("MyProject.fonts.arial.arialbd.ttf");;
case "Arial#i":
return LoadFontData("MyProject.fonts.arial.ariali.ttf");
case "Arial#bi":
return LoadFontData("MyProject.fonts.arial.arialbi.ttf");
}
return null;
}
/// <summary>
/// Returns the specified font from an embedded resource.
/// </summary>
private byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();
// Test code to find the names of embedded fonts - put a watch on "ourResources"
//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;
}
}
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用 PDFsharp 1.50 的 WPF 版本时,您可以IFontResolver在自己的类中实现并将该类的实例分配给GlobalFontSettings.FontResolver.
PDFsharp 1.50 仍在建设中。当它最终完成时,它应该包括 FontResolver 示例。
示例代码可以在 PDFsharp 论坛上找到:
http://forum.pdfsharp.net/viewtopic.php ?p=8961#p8961
注意: XPrivateFontCollection 应该与 GDI 和 WPF 版本一起使用。如果您使用 DocumentPreview 并希望在那里查看字体,则必须使用 XPrivateFontCollection。
IFontResolver 可能是没有用户界面的进程的最佳选择(例如在 Web 服务器上生成 PDF),但它不适用于 GDI 构建。
| 归档时间: |
|
| 查看次数: |
3112 次 |
| 最近记录: |