How to create iText BaseFont based on InputStream

I have a font file "arial.ttf" in a web application and I can only get its contents as an InputStream .

 InputStream inputFont = getResourceAsStream("/resources/arial.ttf"); 

How can I create iText BaseFont based on InputStream ? The createFont method createFont not accept it.

 BaseFont bf = BaseFont.createFont(inputFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

createFont (InputStream, String, boolean) cannot call createFont (java.lang.String, java.lang.String, boolean) in BaseFont.

+5
source share
1 answer

Try the following:

 byte[] bytes = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader() .getResourceAsStream("/resources/arial.ttf")); BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_Harial.ttf, BaseFont.EMBEDDED, true, bytes, null); 

You must specify .ttf in the font name to tell the method that it should interpret it as a ttf format.

+3
source

All Articles