如何使用PDFSharp私人字体 [英] How to use private fonts in PDFSharp

查看:796
本文介绍了如何使用PDFSharp私人字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加文本使用专用的PDF文档,即系统,TrueType字体和遇到问题上未安装的。我使用PDFSharp + MigraDoc WPF版本1.32.2608.0从的NuGet。

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.

我发现在论坛上,并在计算器上众多的物品。最新的是这是指的文章= http://www.pdfsharp.net/wiki/PrivateFonts-sample.ashx相对=nofollow>这个例子的上PdfSharp网站。但是这个例子中包含的代码:

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

this.fontFamilies.Add(key, fontFamily);



第22行,但我无法找到组装fontFamilies任何引用。

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

因此,我遵循什么看上去像一个的极好的例子的,但它不为我工作。当我用这个方法,我可以成功添加的字体,但是当我来​​使用命令:

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



其中, fontStyle.Name 是名称的字体,而不会对前方#。在PdfSharp.Drawing命名空间里的私人无效初始化()方法中这一点PdfSharp休息。

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.

在检查在初始化方法中的变量在这一点上有:

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


  1. 发现字体系列,即 this.family!= NULL

  2. 找到字体,即 this.typeface!= NULL

  1. Found the font family, i.e. this.family != null
  2. Found the typeface, i.e. this.typeface != null

我猜想就行了突破如果(!typeface2.TryGetGlyphTypeface(出字体)),但我不能肯定。

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

请注意:我曾经尝试都一个.TTF和杂项文件字体不用。

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

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

Could someone point me in the right direction please?

在最后我换到 PDFSharp WPF 1.50测试版作为它的字体处理要好得多。看到这个 SO发布我在的这解决了我的问题,新的字体解析。现在一切都运转良好。

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.

推荐答案

好吧,这个问题的答案是相当复杂的,但在情况下,它可以帮助其他人那么这里就是答案。

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

如果你想使用专用字体,例如字体尚未安装在您的系统上,具有PDFSharp那么你需要做到以下几点。

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.

在这个时间点,2015年3月,是唯一发布的NuGet库与私人字体的工作原理是PDFsharp + MigraDoc(WPF)1.32.2608版本。有大量的测试版本为1.50,但这个答案是大约1.32版本。

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.


  1. 您必须使用WPF版本1.32,而不是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)的字体是细

  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.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!
  5. Be aware that TrueType (.ttf) fonts can come in MAC or Windows format - that threw me. OpenType (.otf) fonts are fine.

注意:在字体的名称的最佳途径找到它在Windows中双击字体文件。然后,Windows显示你在第一行的名称的字体。正如我所说,得到错误的,你可以得到一个停顿系统。

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.

最后,我应该说声谢谢@ThomasH谁指示我到PDFSharp 1.32的源代码。这对民营字体的'适当'的例子,也没有那个讨厌的 Debugger.Break 但是适当的异常,当你问的字体名称不。目前

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.

在我交换到的 PDFSharp WPF 1.50公测它的字体处理要好得多。看到这个 SO发布我的问题和信息上的新字体解析这帮助。

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私人字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆