Skip to content

Add flag to ColumnText to control if words are chopped or Throw exception instead of arbitrarily chopping long words#1297#1379

Merged
andreasrosdalw merged 2 commits intoLibrePDF:masterfrom
csimoes1:master
Jun 27, 2025
Merged

Add flag to ColumnText to control if words are chopped or Throw exception instead of arbitrarily chopping long words#1297#1379
andreasrosdalw merged 2 commits intoLibrePDF:masterfrom
csimoes1:master

Conversation

@csimoes1
Copy link
Copy Markdown
Contributor

@csimoes1 csimoes1 commented Jun 26, 2025

Feature to address issue #1297 Throw exception instead of arbitrarily chopping long words

I added a flag to ColumnText that defaults to current behavior, but if the flag setStrictWordWrapping(true) then it throws a StrictWordWrapException when this condition occurs.

Unit-Tests for the new Feature/Bugfix

The following code can be used to reproduce the bug/feature. Toggle column.setStrictWordWrapping() between true and false to test this issue.

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;

public class BugTest {
    public static void main(String[] args) {
        try (Document document = new Document()) {
            FileOutputStream stream = new FileOutputStream("test.pdf");
            PdfWriter writer = PdfWriter.getInstance(document, stream);
            document.open();
            PdfContentByte canvas = writer.getDirectContent();

            canvas.saveState();
            canvas.rectangle(10, 10, 100, 200);
            canvas.setLineWidth(0.1f);
            canvas.stroke();
            canvas.restoreState();

            canvas.saveState();
            ColumnText column = new ColumnText(canvas);
            column.setStrictWordWrapping(true);
            column.setSimpleColumn(10, 10, 110, 210);
            column.addText(new Chunk("German is a language known for long word compositions. One such example is 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', which is German for deoxyribonucleic acid."));
            column.go();
            canvas.restoreState();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Compatibilities Issues

Is anything broken because of the new code? No
Any changes in method signatures? no, but in BidiLine I did have to overload processLine()

Your real name

Please specify your full name here, so that we can verify your identity. - Chris Simoes
If you have a conflict of interest describe this here also. - no conflicts of interest

Testing details

Any other details about how to test the new feature or bugfix? I believe the code above does a good job reproducing the issue.

@sonarqubecloud
Copy link
Copy Markdown

@andreasrosdalw andreasrosdalw merged commit 459ce84 into LibrePDF:master Jun 27, 2025
2 of 4 checks passed
@andreasrosdalw andreasrosdalw changed the title Fix/Feature add for issue #1297 Add flag to ColumnText to control if words are chopped #1297 Jun 27, 2025
@andreasrosdalw andreasrosdalw changed the title Add flag to ColumnText to control if words are chopped #1297 Add flag to ColumnText to control if words are chopped or Throw exception instead of arbitrarily chopping long words#1297 Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants