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?

25th October 2015

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

Share this article


Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now