Load a barcode font for PDFBox + Zxing recognition

Viewed 95

I have a component that gets a pdf file as byte array, loads it using Pdfbox library and then recognizes a barcode using Zxing library. I need to load 2 barcode fonts from a particular directory before recognition, without it the barcode would not be recognized. I tried to do execute this code:

PDDocument document = ...;

PDFont font = PDTrueTypeFont.load(document, new File("...\fontFile.ttf"), Encoding.getInstance(COSName.STANDARD_ENCODING));

PDFRenderer renderer = new PDFRenderer(document);

document.getPage(0).getResources().add(font);
BufferedImage image = renderer.renderImageWIthDPI(0, dpi);

but it doesn't work, the font is not loaded to the right component (I don't know where it should be), the rendered image occurrs to be with a broken barcode and of course it fails during the following recognition.

Also I know that during the rendering java.awt.GraphicsEnvironmenttried is used. I tried to create it and add the font there, but it still doesn't work:

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

env.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("...")));
BufferedImage image = renderer.renderImageWIthDPI(0, dpi);

Thanks in advance for ideas how to make this work. The font should be loaded from the project resources.

0 Answers
Related