本文介绍了是否有可能在PDFBOX证明的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有PDFBOX API中的任何功能,使文字或理由,我们必须做手工?如果手动那么如何使用Java(其背后的逻辑),以证明文本

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)

推荐答案

This旧的答案显示了如何将字符串分解为子装修成一个给定的宽度。为了使样本code有绘制子的方式来填补整个线宽,更换的最终环

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 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);
}

此解决方案仅仅使用额外的字符间距(运营商的)的理由。您可以改为使用额外的字间距(运营商的繁体)其中只有扩大空格字符,或两者的组合。有关这些操作参见更多信息表105的的文本状态运营商的,第9.3.2节的字符间距的,而第9.3.3的字间距的在PDF规范的

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

而不是以前的

您现在获得

正如你所看到还有一个次要的赤字,一个段落的最后一行显然不应该是合理的。在最后一行,因此,使用 0 字符间距,而不是:

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

PS:如果你想知道关于更换('。'',')

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证明的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:47