Break cell content using the LineBreak event

Generate PDF, Manipulate PDF


When the content of a cell is very long and it has no spaces then it cannot be broken/hyphenated by the default hyphenation algorithm. Consequently, the cell will expand to fit the content and potentially expand beyond the page boundary and break the table layout settings. This article shows how to use the LineBreak event to customize hyphenation.

Here is how this looks:

Line break sample.png

You can overcome this by using the LineBreak event of the TextParagraph:

1 textParagraph.LineBreak += new LineBreakEventHandler(textParagraph_LineBreak);

The LineBreak event fires whenever the layout engine has reached the preferred width of the cell. The following LineBreak event handler simply inserts a hyphen two characters to the left of the current position:

1 static void textParagraph_LineBreak(Fragment sender, LineBreakEventArgs args) 2 { 3 args.BreakAt = args.BreakBefore - 2; 4 args.InsertHyphen = true; 5 }

Here is the result:

Line break sample 2.png