Is there any function in PDFBOX API to make text justified or we have to do it manually?? and if manually then how to justify text using java(logic behind it)
A method that can center the text, something like addCenteredString(myString) A method that can give me the width of the string in pixels. I can then calculate the center, for I know the dimensions of the PDF.
Apache PDFBox is an open source Java library that can be used to create, render, print, split, merge, alter, verify and extract text and meta-data of PDF files.
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...
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):
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\n", 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\n", 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...
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\n"); // 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With