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

In iText 7 you can create a PdfFont from a byte[], using the createFont() method. Suppose that fBytes is a byte[], then you'd have:

PdfFont font = PdfFontFactory.createFont(fBytes, PdfEncodings.IDENTITY_H, true);

You can chose different encodings depending on the type of your font. The last parameter is a boolean embedded value.

Now you can use font as usual:

    doc.add(new Paragraph(“Hello World!”).setFont(font).setFontSize(12));

Whether you are working with Type 1 font, you should use createType1Font() method:

PdfFont type1font = PdfFontFactory.createFont(FontProgramFactory.createType1Font(afm, pfb), null);

As you know, Type 1 fonts consist of two files: a metrics file (AFM or PFM) and a font binary (PFB). In the code above afm and pfb are byte arrays.

Click this link if you want to see how to answer this question in iText 5.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

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