Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

java - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
"); // 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...