Skip to main content
Skip table of contents

How to get a font from an array?

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".

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.