mkl-public / testarea-pdfbox2

Test area for public PDFBox v2 issues on stackoverflow etc
Apache License 2.0
83 stars 44 forks source link

One issue with BreakLongString.java example #8

Closed ifindthanh closed 7 months ago

ifindthanh commented 3 years ago

Hi there,

I've posted this question to Stackoverflow but it is deleted by administrator. I'm following the example of BreakLongString.java to show my content in Signature field. The problem is, if the content of my text contains a very long word, this example does not handle this case yet.

For example, this is my text

String text = "I am trying toTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTM create a PDF file with a lot of text contents in the document. I am using PDFBox";

And the text is truncated in the generated pdf file. o9u9s

Please help me to have a look on the issue?

mkl-public commented 7 months ago

Indeed, BreakLongString does not break overlong words but lets them fill a line by themselves for as long as they are. The code position deciding this is

                if (size > width)
                {
                    if (lastSpace < 0)
                        lastSpace = spaceIndex;

If you want a different handling of overlong words, you have to change the lastSpace = spaceIndex; to setting lasspace to some smaller number. How you choose that number, depends on your preferred word breaking approach.

Discussing word breaking approaches here clearly exceeded the scope of a stack overflow question. Seeing the current implementation, though, it should be easy to extend the code accordingly.