I'm developing an application that must create a PDF file with different font styles (sometimes bold, sometimes italic and sometimes regular). The Font that I must use is Eras Medium BT (True Type), and I load it using a local file named "erasm.TTF". My question is, how can I draw text in bold or italics using my Eras font file?
I've got a legacy code that uses iText to generate a similar PDF, and to get a bold Font I just need to call this function:
public Font getFontErasMDBTBold9(){
FontFactory.register(fontPath + "erasm.TTF", "ERASM");
fontErasMDBT9 = FontFactory.getFont("ERASM", 9, Font.BOLD, Color.BLACK);
return fontErasMDBT9;
}
Edit: I've seen in other questions that it can be done using different font variants, or artificially by using raw commands. What I want is to use the original font and set some text to be bold, other text italics and the rest just regular.
Is it possible to open a Font in bold style or italic style like in iText?
Thanks for your comments and advices. Finally I used the setRenderingMode method of the PDFPageContentStream class to set the different styles of my text. Here's a private method to write some text with the desired render mode:
private void writeText(PDPageContentStream contentStream, String text, PDFont font,
int size, float xPos, float yPos, RenderingMode renderMode = RenderingMode.FILL) {
contentStream.beginText()
contentStream.setFont(font, size)
contentStream.newLineAtOffset(xPos, yPos)
contentStream.setRenderingMode(renderMode)
contentStream.showText(text)
contentStream.endText()
}
And here is the code to write regular text and bold text.
private void addFrontPage(PDDocument document) {
PDPage frontPage = newPage()
PDPageContentStream contentStream = new PDPageContentStream(document, frontPage)
// Write text
String text = "This is a bold text"
writeText(contentStream, text, eras, 18, 25, 500, RenderingMode.FILL_STROKE)
text = "and this is a regular text"
writeText(contentStream, text, eras, 9, 25, 480)
contentStream.close()
document.addPage(frontPage)
}
Note: The code is writen in Groovy language.