2

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?

Fran
  • 41
  • 6
  • 2
    Possible duplicate of [PDFBOX Same Stream with bold and normal text](https://stackoverflow.com/questions/21561298/pdfbox-same-stream-with-bold-and-normal-text) – Jaydip Rakholiya Oct 01 '18 at 15:45
  • On the OS level, bold, italic and italic+bold are really different font files (or one file as a "font collection" that contains these fonts). For example, do "dir arial*" in c:\windows\fonts if you are using windows. – Tilman Hausherr Oct 02 '18 at 07:39
  • Concerning your edit - *"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."* - pdfbox does not have a high level text styling API. Using other font files or changing the graphics state implicitly is what such a high level API would do for you. Thus, you have to do this explicitly. – mkl Oct 02 '18 at 12:55

2 Answers2

2

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.

Fran
  • 41
  • 6
1

Here is a full example to explain how to render the used font to appear in Italic & bold:

String message = "This is a message in the page.";
PDDocument document = new PDDocument();
PDPage page = new PDPage();

PDPageContentStream contentStream = new PDPageContentStream( document, page, AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont( font, fontSize );  // set font and font size.
contentStream.setNonStrokingColor( 1f, 0, 0 );  // set text color to red

// Modify font to appear in Italic:
Matrix matrix = new Matrix( 1, 0, .2f, 1, 7, 5 );
contentStream.setTextMatrix( matrix );

// Modify the font to appear in bold:
contentStream.setRenderingMode( RenderingMode.FILL_STROKE );
contentStream.setStrokingColor( 1f, 0, 0 );

// Write text:
contentStream.showText( message );
contentStream.endText();
contentStream.close();

document.addPage( page );
document.save( PDF_FILE_PATH );
document.close();
Brad
  • 4,457
  • 10
  • 56
  • 93