How to get a font from an array?

Category: 
Tags: fontsbyte arrayiText 5

Is it possible to create a Font object from a byte array instead of using file paths? The FontFactory class has two methods for registering fonts. Both use file/folder paths to register font.

Posted on StackOverflow on May 6, 2015 by Robert

Yes, you can create a Font from a byte[], but in that case, you can't use the FontFactory. Instead you need to create a BaseFont instance using the createFont method,

Once you have a BaseFont instance, you can easily create a Font object:

Suppose that fBytes is a byte[], then you'd have:

BaseFont bf = BaseFont.createFont(
    "myFont", BaseFont.IDENTITY_H, BaseFont.EMBEDDED,
    true, fBytes, null);
Font font = new Font(bf, 12);

The method accepts two byte[] parameters because Type 1 fonts consist of two files: a metrics file (AFM or PFM) and a font binary (PFB). For all other fonts (TTF, OTF,...), the second byte[] parameter should be null.

There is currently no way to add such a font to the FontFactory as a "registered font".