如何在PDFSharp中使用私有字体

时间:2022-09-07 08:17:27

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.

我试图使用私有文本添加文本到PDF文档,即没有安装在系统上,TrueType字体和麻烦。我正在使用NuGet的PDFSharp + MigraDoc WPF版本1.32.2608.0。

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

我在论坛和*上找到了很多项目。最新的是这篇*文章,它引用了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.Name is 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是前面没有#的字体名称。此时,PdfSharp在PdfSharp.Drawing命名空间内的private void Initialise()方法内部中断。

Inspecting the variables inside the Initialise method at this point it has:

此时检查Initialise方法中的变量它具有:

  1. Found the font family, i.e. this.family != null
  2. 找到字体系列,即this.family!= null

  3. Found the typeface, i.e. this.typeface != null
  4. 找到了字体,即this.typeface!= null

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

我假设它在线上断开如果(!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 beta as its font handling is MUCH better. See this SO post on my second issues and information on the new font resolver which solved my problem. Everything is working well now.

最后我换成了PDFSharp WPF 1.50 beta,因为它的字体处理要好得多。在我的第二个问题上看到这篇SO帖子,以及解决我问题的新字体解析器的信息。现在一切都很好。

3 个解决方案

#1


5  

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. 您必须使用1.32的WPF版本,而不是GDI +版本。

  3. 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.
  4. 我在PDFSharp页面中发现名为“私有字体”的代码具有误导性。根据ThomasH的说法,PDFSharp团队希望人们下载样本,样本中的代码片段非常不同。

  5. 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.
  6. 有关如何编写代码的最佳示例,请访问http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880#p5395。注意:作者关于加载字体两次的说明导致异常是正确的。他的处理方法很有效,但速度很慢。我预扫描所有字体并按名称分组,以便只添加一次。

  7. 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.Break instead 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!
  8. 要非常小心字体的名称(请参阅结尾处的注释)。很容易让名字出错,如果你这么做,你会遇到各种各样的麻烦。发布的NuGet版本有一个令人讨厌的Debugger.Break而不是异常,并且在发布的代码中它只是停顿 - 在我的单元测试中我必须关闭Visual Studio才能摆脱它!

  9. Be aware that TrueType (.ttf) fonts can come in MAC or Windows format - that threw me. OpenType (.otf) fonts are fine.
  10. 请注意,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.Break but 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 beta as its font handling is MUCH better. See this SO post on my problem and information on the new font resolver which helped.

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

#2


4  

And for PDFsharp 1.50 you have to implement the IFontResolver interface 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: ResolveTypeface and GetFont.

IFontResolver接口需要两个方法:ResolveTypeface和GetFont。

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文件。

#3


2  

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://*.com/a/21545488/1015447

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

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

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

#1


5  

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. 您必须使用1.32的WPF版本,而不是GDI +版本。

  3. 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.
  4. 我在PDFSharp页面中发现名为“私有字体”的代码具有误导性。根据ThomasH的说法,PDFSharp团队希望人们下载样本,样本中的代码片段非常不同。

  5. 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.
  6. 有关如何编写代码的最佳示例,请访问http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880#p5395。注意:作者关于加载字体两次的说明导致异常是正确的。他的处理方法很有效,但速度很慢。我预扫描所有字体并按名称分组,以便只添加一次。

  7. 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.Break instead 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!
  8. 要非常小心字体的名称(请参阅结尾处的注释)。很容易让名字出错,如果你这么做,你会遇到各种各样的麻烦。发布的NuGet版本有一个令人讨厌的Debugger.Break而不是异常,并且在发布的代码中它只是停顿 - 在我的单元测试中我必须关闭Visual Studio才能摆脱它!

  9. Be aware that TrueType (.ttf) fonts can come in MAC or Windows format - that threw me. OpenType (.otf) fonts are fine.
  10. 请注意,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.Break but 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 beta as its font handling is MUCH better. See this SO post on my problem and information on the new font resolver which helped.

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

#2


4  

And for PDFsharp 1.50 you have to implement the IFontResolver interface 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: ResolveTypeface and GetFont.

IFontResolver接口需要两个方法:ResolveTypeface和GetFont。

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文件。

#3


2  

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://*.com/a/21545488/1015447

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

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

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