wpf 如何在 PDFSharp 中使用私有字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28996492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:00:19  来源:igfitidea点击:

How to use private fonts in PDFSharp

c#wpffontspdfsharp

提问by Jon P Smith

I am trying to add text to a PDF document using private, i.e. not installed on the system, TrueType fonts and having trouble. I am using PDFSharp + MigraDoc WPF version 1.32.2608.0 from NuGet.

我正在尝试使用私有(即系统上未安装)TrueType 字体将文本添加到 PDF 文档并且遇到问题。我正在使用 NuGet 的 PDFSharp + MigraDoc WPF 1.32.2608.0 版。

I have found numerous items on the forum and on StackOverFlow. The latest is this stackoverflowarticle which refers to this exampleon the PdfSharp site. However this example contains the code:

我在论坛和 StackOverFlow 上找到了许多项目。最新的是这篇 stackoverflow文章,它引用了 PdfSharp 站点上的这个示例。但是,此示例包含代码:

this.fontFamilies.Add(key, fontFamily);

on line 22, but I cannot find any reference to fontFamilies in assembly.

在第 22 行,但我在程序集中找不到任何对 fontFamilies 的引用。

I therefore have followed what looked like an excellent example, but it does not work for me. When I use this approach I can successfully add the font but when I come to use the command:

因此,我遵循了一个看起来很好的例子,但它对我不起作用。当我使用这种方法时,我可以成功添加字体,但是当我开始使用命令时:

var font = new XFont(fontStyle.Name, new XUnit(fontStyle.SizePt, XGraphicsUnit.Point), XFontStyle.Regular, _fontOptions);

Where fontStyle.Nameis the name of the font without the # on the front. At this point PdfSharp breaks inside the private void Initialise()method inside the PdfSharp.Drawing namespace.

fontStyle.Name前面没有# 的字体名称在哪里。此时 PdfSharpprivate void Initialise()在 PdfSharp.Drawing 命名空间内的方法内部中断。

Inspecting the variables inside the Initialisemethod at this point it has:

此时检查Initialise方法内部的变量它有:

  1. Found the font family, i.e. this.family != null
  2. Found the typeface, i.e. this.typeface != null
  1. 找到字体家族,即 this.family != null
  2. 找到字体,即 this.typeface != null

I am assuming it break on the line if (!typeface2.TryGetGlyphTypeface(out typeface))but I can't be sure.

我假设它断线了,if (!typeface2.TryGetGlyphTypeface(out typeface))但我不能确定。

Note: I have tried both a .ttf and a .otf font to no avail.

注意:我尝试了 .ttf 和 .otf 字体都无济于事。

Could someone point me in the right direction please?

有人能指出我正确的方向吗?

Update

更新

In the end I swapped to PDFSharp WPF 1.50 betaas its font handling is MUCH better. See this SO poston my second issues and information on the new font resolverwhich solved my problem. Everything is working well now.

最后我换成了PDFSharp WPF 1.50 beta,因为它的字体处理要好得多。请参阅有关我的第二个问题的SO 帖子以及有关解决我的问题的新字体解析器的信息。现在一切正常。

回答by Jon P Smith

Ok, the answer to this is fairly complex, but in case it helps someone else then here is the answer.

好的,这个问题的答案相当复杂,但如果它对其他人有帮助,那么这就是答案。

If you want to use private fonts, i.e. fonts not already installed on you system, with PDFSharp then you need to do the following.

如果您想使用私有字体,即系统上尚未安装的字体,则需要使用 PDFSharp 执行以下操作。

At this point in time, March 2015, the only released NuGet library that works with private fonts is the PDFsharp + MigraDoc (WPF) 1.32.2608 release. There are plenty of beta releases for 1.50 but this answer is about 1.32 version.

此时,即 2015 年 3 月,唯一发布的适用于私有字体的 NuGet 库是 PDFsharp + MigraDoc (WPF) 1.32.2608 版本。1.50 有很多 beta 版本,但这个答案是关于 1.32 版本的。

  1. You MUST use the WPF version of 1.32, not the GDI+ version.
  2. I found the code in the PDFSharp page called 'Private Fonts' misleading. According to ThomasH the PDFSharp team expected people to download the sample, which has a very different piece of code in its sample.
  3. The best example of how to write the code can be found at http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880#p5395.
    Note:the author's note about loading a font twice causes an exception is correct. His method of handling this works, but it is slow. I pre-scan all my fonts and group them by name so that only add them once.
  4. Be VERY careful about the name of the font(see note at end). It is very easy to get the name wrong and if you do you get into all sorts of trouble. The released NuGet version has a nasty Debugger.Breakinstead of an exception and in released code it just stalls - In my Unit Tests I have to close Visual Studio to get out of it!
  5. Be aware that TrueType (.ttf) fonts can come in MAC or Windows format - that threw me. OpenType (.otf) fonts are fine.
  1. 您必须使用 1.32 的 WPF 版本,而不是 GDI+ 版本。
  2. 我发现 PDFSharp 页面中名为“私人字体”的代码具有误导性。根据 ThomasH 的说法,PDFSharp 团队希望人们下载该示例,它的示例中有一段非常不同的代码。
  3. 可以在http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880#p5395找到如何编写代码的最佳示例。
    注意:作者关于加载字体两次导致异常的注释是正确的。他处理这个问题的方法有效,但速度很慢。我预先扫描所有字体并按名称对它们进行分组,以便只添加一次。
  4. 非常小心的字体的名称(见注月底)。很容易把名字弄错,如果你弄错了,你就会遇到各种各样的麻烦。已发布的 NuGet 版本有一个令人讨厌的Debugger.Break而不是异常,并且在已发布的代码中它只是停顿 - 在我的单元测试中,我必须关闭 Visual Studio 才能摆脱它!
  5. 请注意,TrueType (.ttf) 字体可以采用 MAC 或 Windows 格式——这让我很困惑。OpenType (.otf) 字体很好。

NOTE:On the name of the font the best way to find it in Windows is to double click the font file. Windows then shows you the font with the name on the first line. As I say, get that wrong and you can get a stalled system.

注意:在字体名称上,在 Windows 中找到它的最佳方法是双击字体文件。然后,Windows 会在第一行显示带有名称的字体。正如我所说,如果弄错了,您可能会遇到停滞不前的系统。

Finally I should say thank you to @ThomasH who directed me to the PDFSharp 1.32 source code. This has the 'proper' example for private fonts and also doesn't have that nasty Debugger.Breakbut the proper exception when the name of the font you asked for isn't present.

最后,我要感谢 @ThomasH,他将我引导至 PDFSharp 1.32 源代码。这有私人字体的“正确”示例,并且Debugger.Break当您要求的字体名称不存在时,也没有那种讨厌但正确的例外。

Update

更新

In the end I swapped to PDFSharp WPF 1.50 betaas its font handling is MUCH better. See this SO poston my problem and information on the new font resolverwhich helped.

最后我换成了PDFSharp WPF 1.50 beta,因为它的字体处理要好得多。请参阅有关我的问题的SO 帖子以及有关新字体解析器的信息,这有帮助。

回答by I liked the old Stack Overflow

And for PDFsharp 1.50you have to implement the IFontResolverinterface and assign your implementation to a global PDFsharp property.

对于 PDFsharp 1.50,您必须实现IFontResolver接口并将您的实现分配给全局 PDFsharp 属性。

GlobalFontSettings.FontResolver = new DemoFontResolver();

PDFsharp provides a class FontResolverBase that you can use to derive your own class. Just add code for your private fonts and pass the call to the baseclass for other fonts.

PDFsharp 提供了一个 FontResolverBase 类,您可以使用它来派生自己的类。只需为您的私有字体添加代码并将调用传递给其他字体的基类。

The IFontResolver interface requires two methods: ResolveTypefaceand GetFont.

IFontResolver 接口需要两个方法:ResolveTypefaceGetFont

More information can be found in my blog post:
http://developer.th-soft.com/developer/?p=11

更多信息可以在我的博客文章中找到:http:
//developer.th-soft.com/developer/?p=11

A complete solution is provided there as a ZIP file.

那里以 ZIP 文件的形式提供了完整的解决方案。

回答by I liked the old Stack Overflow

Re "line 22": The PDFsharp forum just shows a snippet from the sample.

重新“第 22 行”:PDFsharp 论坛仅显示示例中的一个片段。

For PDFsharp 1.3x:You can download the complete sample code from CodePlex or SourceForge with the full working sample (project, solution, sample TTF files all included).

对于 PDFsharp 1.3x:您可以从 CodePlex 或 SourceForge 下载完整的示例代码以及完整的工作示例(项目、解决方案、示例 TTF 文件都包括在内)。

See also:
https://stackoverflow.com/a/21545488/1015447

另见:https:
//stackoverflow.com/a/21545488/1015447

Complete source code for version 1.32:
http://pdfsharp.codeplex.com/releases

1.32 版的完整源代码:http:
//pdfsharp.codeplex.com/releases