How to go to the next line when adding text using Apache PDFBox

I just started using Apache PDFBox and experimented with various examples that I found.

However, I could not find an easy way to go to the next line when adding text.

eg.

PDPageContentStream content = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA; content.beginText(); content.setFont(font, 12); content.moveTextPositionByAmount(x, y); content.drawString("Some text."); content.endText(); 

To add another line of text, I had to experiment with the y value in moveTextPositionByAmount until it overwrites the previous line.

Is there a more intuitive way to figure out what the coordinates of the next line are?

TIA

+6
source share
2 answers

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:

 /** * Sets the text leading. * * @param leading The leading in unscaled text units. * @throws IOException If there is an error writing to the stream. */ public void setLeading(double leading) throws IOException { writeOperand((float) leading); writeOperator("TL"); } /** * Move to the start of the next line of text. Requires the leading to have been set. * * @throws IOException If there is an error writing to the stream. */ 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*");

+7
source

add a new line with a y-axis offset like this

 PDPageContentStream content = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA; content.beginText(); content.setFont(font, 12); // by default y = 0 pdf text start in the left bottom corner // so you may need to put y = 700 or something to see the new line below content.moveTextPositionByAmount(x, y); content.drawString("Some text."); content.newLineAtOffset(0, -15); content.drawString("some text "); content.endText(); 
0
source

Source: https://habr.com/ru/post/980696/


All Articles