自定义字体中缺少字符 [英] Missing character in custom font
问题描述
我们正在使用iTextSharp使用自定义字体创建PDF,而我遇到了unicode字符2120(SM,Service Mark)的问题。问题是字形不是自定义字体。有没有办法可以为PDF中的字段指定后备字体?我们尝试使用Verdana添加一个文本字段,以便表单中嵌入了辅助字体,但似乎没有帮助。
We are using iTextSharp to create PDF's using a custom font and I am running into an issue with the unicode character 2120 (SM, Service Mark). The problem is the glyph is not in the custom font. Is there a way I can specify a fallback font for a field in a PDF? We tried adding a text field with Verdana so that the form had the secondary font embedded in it but that didn't seem to help.
推荐答案
首先,您需要找到具有所需字符的字体(ttf,otf,...)。然后,您可以使用 FontSelector
类,并将要使用的不同字体添加到此选择器中(请参阅 FontSelectionExample )。现在你可以处理每个字符串:
First you need to find a font (ttf, otf,...) that has the character you need. Then you can use the FontSelector
class and add the different fonts you want to use to this selector (see the FontSelectionExample). Now you can process every string:
FontSelector selector = new FontSelector();
selector.addFont(f1);
selector.addFont(f2);
Phrase ph = selector.process(some_string);
FontSelector
将返回一个包含的短语 Chunk
的字体 f1
对于第一种字体中可用的所有字形,以及 Chunk
s,字体 f2
,表示字体不能是 f1
中的字体,但它出现在 f2
。
The FontSelector
will return a phrase that consists of Chunk
s with font f1
for all the glyphs that are available in the first font, and Chunk
s with font f2
for the glyphs that couldn't be font in f1
, but that are present in f2
.
如果你想要这个例子的C#端口,请咨询第11章。
If you want the C# port of this example, please consult chapter 11.
更新
使用 FontSelector
在表单的上下文中工作(只要我们谈论AcroForm技术)。这很简单:你只需要在表单中添加替换字体:
Using a FontSelector
also works in the context of forms (as long as we're talking about AcroForm technology). It's really easy: you just need to add substitutions fonts to the form:
AcroFields form = stamper.getAcroFields();
form.addSubstitutionFont(bf1);
form.addSubstitutionFont(bf2);
现在表单中定义的字体有首选项,但如果该字体无法显示特定字形,它将查看 bf1
,然后是 bf2
,依此类推。您可以在此处找到演示此功能的示例。
Now the font defined in the form has preference, but if that font can't show a specific glyph, it will look at bf1
, then at bf2
and so on. You can find an example demonstrating this functionality here.
请注意,第一个和第二个示例之间存在差异。在第一个例子中,我们使用 Font
对象,在第二个例子中,我们使用 BaseFont
对象。
Note that there's a difference between the first and the second example. In the first example we use a Font
object, in the second, we use a BaseFont
object.
这篇关于自定义字体中缺少字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!