How to get a font from an array?

Tags: fontsbyte arrayiText 7

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.