问题
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.
回答1:
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);
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
.
If you want the C# port of this example, please consult chapter 11.
Update
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);
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.
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.
来源:https://stackoverflow.com/questions/19845688/missing-character-in-custom-font