This older answer shows how to break a string into substrings fitting into a given width
. To make the sample code there draw the substrings in a manner to fill the whole line widths, replace as follows (depending on the PDFBox version):
PDFBox 1.8.x
Replace the final loop
for (String line: lines)
{
contentStream.drawString(line);
contentStream.moveTextPositionByAmount(0, -leading);
}
with this more elaborate one:
for (String line: lines)
{
float charSpacing = 0;
if (line.length() > 1)
{
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0)
{
charSpacing = free / (line.length() - 1);
}
}
contentStream.appendRawCommands(String.format("%f Tc
", charSpacing).replace(',', '.'));
contentStream.drawString(line);
contentStream.moveTextPositionByAmount(0, -leading);
}
(From BreakLongString.java test testBreakStringJustified
for PDFBox 1.8.x)
If you are wondering about the replace(',', '.')
in
contentStream.appendRawCommands(String.format("%f Tc
", charSpacing).replace(',', '.'));
... my locale uses a comma as decimals separator, and after my first test run resulted in commas in the page content, I was a bit lazy and simply added that replace to fix things...
PDFBox 2.0.x
Replace the final loop
for (String line: lines)
{
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}
with this more elaborate one:
for (String line: lines)
{
float charSpacing = 0;
if (line.length() > 1)
{
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0)
{
charSpacing = free / (line.length() - 1);
}
}
contentStream.setCharacterSpacing(charSpacing);
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}
(From BreakLongString.java test testBreakStringJustified
for PDFBox 2.0.x)
This solution merely uses extra character spacing (operator Tc) for justification. You might instead use extra word spacing (operator Tw) which only expands space characters, or a combination of both; beware, though: word spacing does not work with all font encodings. For more information on these operands cf. Table 105 Text state operators, section 9.3.2 Character Spacing, and section 9.3.3 Word Spacing in the PDF specification ISO 32000-1
Instead of the former
you now get
As you see there is still one minor deficit, the last line of a paragraph obviously should not be justified. In the last line, therefore, use a 0
character spacing instead:
contentStream.appendRawCommands("0 Tc
"); // PDFBox 1.8.x
contentStream.setCharacterSpacing(0); // PDFBox 2.0.x
PS I just stumbled across the fact that the setCharacterSpacing
currently (November 2016) only is in the 2.1.0-SNAPSHOT development version and not the 2.0.x release versions yet. Thus, in 2.0.x you might have to fall back to using appendRawCommands
instead, even if it had been marked deprecated.