Search code examples
wpfflowdocument

How to show a part of a FlowDocument?


I need to shorten a FlowDocument which is shown in a FlowDocumentScrollViewer.

shows: Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

should show: Lorem ipsu...

How can I do this?


Solution

  • I did it as the following. The FlowDocument is trimmed if it is longer than "maxLength":

    var wholeTextRange = new TextRange(doc.ContentStart, doc.ContentEnd);
    if (wholeTextRange.Text.Length > maxLength)
    {
        TextPointer endPos = doc.ContentStart.GetPositionAtOffset(maxLength);
        var textRange = new TextRange(endPos, doc.ContentEnd);
        textRange.Text = "...";
    }