【问题标题】:In iText 7, How to know whether specific character exists in font?在 iText 7 中,如何知道字体中是否存在特定字符?
【发布时间】:2017-08-31 08:26:13
【问题描述】:

在 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 中可行吗?

【问题讨论】:

    标签: fonts character itext7


    【解决方案1】:

    当然

    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){}
    }
    

    这会打印如下内容:

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

    【讨论】:

    • 非常感谢,乔里斯。这很有帮助。
    • 那么,这是公认的答案吗?提示提示...:D
    【解决方案2】:

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

    public abstract Glyph getGlyph(int unicode);
    

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

    【讨论】:

    • containsGlyph(由@Joris 提议)调用getGlyph 并且 如果返回Glyph 实例,则进行额外测试
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2019-11-21
    • 2021-09-19
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    相关资源
    最近更新 更多