I am using PDFBox in my java application to generate PDF files and I am a newbie in this area. I have a requirement, where the output text content is in a variable. The output text should be a mixed of both BOLD and Normal fonts.
Code Snippet
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
float xStartPos = 100;
float yStartPos = 650;
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( xStartPos, yStartPos );
String displayText = "Hello world";
contentStream.drawString(displayText);
contentStream.endText();
In the above code Hello should be in bold font and world should be in normal font. My requirement is using the single drawString call, this should be achieved. Any one can help?
Thanks in advance.
A single
drawString
method call to generate both normal and bold font (as you mention in the question headline) is not possible becausePDPageContentStream.drawString
essentially creates one operation in the PDF drawing the whole parameter string using the current font. Thus, multipledrawString
calls are required.In your case e.g.: