The PDFBox API allows you to create low-level content. This implies that you should do (but also what you can do) most of the layout yourself, among which you decide how much you need to move down to go to the next baseline.
This distance (called leading in this context) depends on a number of factors:
- font size used (obviously)
- How tight or loose the text is.
- the presence of elements on lines located outside the regular line, for example. superscripts, indexes, formulas, ...
The standard is designed so that the nominal height of tightly spaced lines of text is 1 unit for a font drawn in size 1. Thus, you will usually use a leader 1.1.5 times the font size, if only there is material on the line that extends beyond it .
By the way, if you need to forward the next line very often for the same amount, you can use a combination of the PDPageContentStream setLeading and newLine instead of moveTextPositionByAmount :
content.setFont(font, 12); content.setLeading(14.5f); content.moveTextPositionByAmount(x, y); content.drawString("Some text."); content.newLine(); content.drawString("Some more text."); content.newLine(); content.drawString("Still some more text.");
PS: It seems that moveTextPositionByAmount will be deprecated in version 2.0.0 and will be replaced by newLineAtOffset .
PPS: as OP points out in the comment,
There is no PDPageContentStream method called setLeading. I am using PDFBox version 1.8.8.
In fact, I watched the current version 2.0.0-SNAPSHOT. Currently they are performed as follows:
public void setLeading(double leading) throws IOException { writeOperand((float) leading); writeOperator("TL"); } public void newLine() throws IOException { if (!inTextMode) { throw new IllegalStateException("Must call beginText() before newLine()"); } writeOperator("T*"); }
You can easily implement external helper methods that perform the equivalent using appendRawCommands((float) leading); appendRawCommands(" TL"); appendRawCommands((float) leading); appendRawCommands(" TL"); and appendRawCommands("T*");