如何使用 iText 2.1.3 打印字体中不存在的字符

How to print characters not existing in a font using iText 2.1.3

我正在编写一个使用 iText 2.1.3 生成 PDF 文件的应用程序。当打印包含不在所用字体中的字符的 Unicode 文本时,这些字符会在输出中消失。

我读到解决这个问题的方法是使用 FontSelector 像这样的东西:

public void createPdf(final String filename, final String text)
  throws IOException, DocumentException {
    // Get at document for printing.
    final Document document = new Document(...);
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();

    // Create a FontSelector with multiple fonts.
    final FontSelector selector = new FontSelector();
    final Font f1 = ...; // Covers e.g. Western glyphs
    selector.addFont(f1);
    final Font f2 = ...; // Covers e.g. Chinese glyphs
    selector.addFont(f2);

    // Phrase contains of chunks each using an approriate font
    // for rendering part of the text.
    final Phrase ph = selector.process(text);
    document.add(new Paragraph(ph));

    document.close();
}

(从 this 示例重写的示例。)

但是,文本中可能仍有一些字符未被任何可用字体覆盖。

有没有办法告诉 iText 2.1.3 打印,例如此类字符的 Unicode 替换字符 (U+FFFD aka )?

使用 Unicode Consortium Last Resort Font 作为按 FontSelector 搜索的最后一个字体,每个可能的 Unicode 字符都有一个字形。

Last Resort Font 是一种特殊字体,它针对不同的字符类别具有不同的字形,可以很好地提示您的字体不支持哪种字符。

因此,PDF 中将显示 here 所示的字形,而不是获取 � 字形。