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

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));

When you are working with Type 1 fonts, you should use the 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 How to get a font from an array? if you want to see how to answer this question in iText 5.

JavaScript errors detected

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

If this problem persists, please contact our support.