Reputation: 33
Good morning!
I have a newbie PDFbox question which I'm hoping for some help with ...
I've just (last week) downloaded the latest PDFbox source from github and am trying to create a HelloWorldOTF.java, based on the HelloWorldTTF.java example, with the hope of creating a PDF file which uses an OTF font (in this case, Adobe Caslon Pro Regular) to add text to the output PDF.
Here's what I have so far:
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
String testOtfFontFile = "c:/windows/fonts/ACaslonPro-Regular.otf";
String testTtfFontFile = "c:/windows/fonts/arial.ttf";
String testPdfFile = "c:/tmp/pdfboxtest.pdf";
CFFFont font = loadCFFFont(testOtfFontFile);
PDFont ttfFont = PDTrueTypeFont.loadTTF(doc, new File(testTtfFontFile));
PDPageContentStream contentStream = new PDPageContentStream(doc,
page);
contentStream.beginText();
// How to set the CFFFont?
contentStream.setFont(ttfFont, 12);
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString(text);
contentStream.endText();
contentStream.close();
doc.save(testPdfFile);
System.out.println(testPdfFile + " created!");
I can load a CFFFont using this code: (loadCFFFont()):
CFFFont cff = null;
input = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(input);
CFFParser cffParser = new CFFParser();
cff = cffParser.parse(bytes).get(0);
... but can't for the life of me figure out how to get from a CFFFont to a PDFont in order to be able to use it to set the font via setFont().
Any help or pointers would be hugely appreciated ...
Thanks a million for reading this far ;)
Upvotes: 3
Views: 3824
Reputation: 61
Using a byte array (which contains the binary data of a font file) to create a PDFont instance:
PDFont font;
if (fontName.toLowerCase(Locale.ROOT).endsWith(".otf")) {
OpenTypeFont otf = new OTFParser().parse(new RandomAccessReadBuffer(new ByteArrayInputStream(fontContent)));
font = PDType0Font.load(document, otf, false);
} else {
font = PDType0Font.load(document, new ByteArrayInputStream(fontContent));
}
Upvotes: 1
Reputation: 31
It worked for me by referring to this link.
Using OTFParser to covert otf to ttf.
OTFParser otfParser = new OTFParser();
OpenTypeFont otf = otfParser.parse(new File("C:/Users/beder/Downloads/code/CODE Light.otf"));
PDFont font = PDType0Font.load(document, otf, false);
Upvotes: 3