In iText 7, How to know whether specific character exists in font?

248 Views Asked by At

In iText 7, How to know whether specific character exists in font?

In iText 5, I used below code.

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

Is this possible in iText 7?

2

There are 2 best solutions below

2
On BEST ANSWER

Of course

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

This prints something like:

Font AGENCYB.TTF has the required glyph.
Font AGENCYR.TTF has the required glyph.
Font ALGER.TTF has the required glyph.
Font ANTQUAB.TTF has the required glyph.
...

1
On

I'm not sure it will work for any font, because I haven't tested them all, but my first attempt would be to use the following method in PdfFont:

public abstract Glyph getGlyph(int unicode);

If the font does not contain a glyph for this Unicode code point, then this method should return null.