Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to justify text in PDFBOX?

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)

like image 490
Neha Singh Avatar asked Dec 19 '13 11:12

Neha Singh


People also ask

How do I align text in a PDFBox?

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.

What is PDFBox used for?

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.

Is PDFBox free for commercial use?

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 ...


1 Answers

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\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...

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

non-justified

you now get

enter image description here

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.

like image 187
mkl Avatar answered Sep 20 '22 04:09

mkl