在iText 7中,如何知道字体中是否存在特定字符?

时间:2017-08-31 08:26:13

标签: fonts character itext7

在iText 7中,如何知道字体中是否存在特定字符?

在iText 5中,我使用了以下代码。

Font font = FontFactory.getFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseFont baseFont = font.getBaseFont();
boolean isExist = baseFont.charExists(ch);

这可以在iText 7中使用吗?

2 个答案:

答案 0 :(得分:3)

当然

File windowsFontDir = new File("C:\\Windows\\Fonts");
for(File fontFile : windowsFontDir.listFiles()) {
    try {
        PdfFont font = PdfFontFactory.createFont(fontFile.getAbsolutePath());
        if(font.containsGlyph((int) 'a'))
        {
            System.out.println("Font " + fontFile.getName() + " has the required glyph.");
        }else
        {
            System.out.println("Font " + fontFile.getName() + " does NOT have the required glyph.");
        }
    }catch(Exception ex){}
}

这打印如下:

  

Font AGENCYB.TTF具有所需的字形   字体AGENCYR.TTF具有所需的字形   字体ALGER.TTF具有所需的字形   字体ANTQUAB.TTF具有所需的字形   ...

答案 1 :(得分:1)

我不确定它是否适用于任何字体,因为我没有对它们进行全部测试,但我的第一次尝试是在PdfFont中使用以下方法:

public abstract Glyph getGlyph(int unicode);

如果字体不包含此Unicode代码点的字形,则此方法应返回null

相关问题