2017-08-31 174 views
1

在iText中7,如何知道字體中是否存在特定字符?在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?

回答

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

這將打印類似:

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

+0

非常感謝你,里斯一個額外的測試。這非常有幫助。 – Franken

+0

那麼,這是被接受的答案呢?提示提示...:D –

1

我不知道它會爲任何字體的工作,因爲我沒有測試過他們,但我第一次嘗試將使用PdfFont下面的方法:如果字體不

public abstract Glyph getGlyph(int unicode); 

不包含此Unicode代碼點的字形,則此方法應返回null

+0

'containsGlyph'(由@Joris建議)調用'getGlyph' *和*使得如果'Glyph'實例返回 – mkl

相關問題