ComponentOne Word for WPF
Working with Word for WPF / Advanced Level Working / Adding Text Flow
In This Topic
    Adding Text Flow
    In This Topic

    You can use text flow in a word document. Using Word for WPF, you can flow text into columns and pages of a document.

    Note that a class named WordUtils is used in the code given below. It is available in the product sample located at the following location on your system:
    Documents\ComponentOne Samples\WPF\WordCreator
    You can use these classes in your application from the mentioned location.

    The following code shows how the text flow feature can be used in Word for WPF:

    ' load long string from resource file
    Dim text As String = "Resource not found..."
    Using sr = New StreamReader(DataAccess.GetStream("flow.txt"))
            text = sr.ReadToEnd()
    End Using
    text = text.Replace(vbTab, "   ")
    
    ' create pdf document
    word.Info.Title = "Text Flow"
    word.LineBreak()
    
    ' add title
    Dim titleFont As New Font("Tahoma", 24, RtfFontStyle.Bold)
    Dim bodyFont As New Font("Tahoma", 9)
    Dim rcPage As Rect = WordUtils.PageRectangle(word)
    Dim paragraph = New RtfParagraph()
    Dim title = New RtfString(word.Info.Title, titleFont, RtfUnderlineStyle.Dotted)
    paragraph.Add(title)
    word.Add(paragraph)
    word.LineBreak()
    word.LineBreak()
    
    ' render string spanning columns and pages
    For Each s As var In text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
            word.AddParagraph(s, bodyFont, Colors.Black, RtfHorizontalAlignment.Justify)
    Next
    
    // load long string from resource file
    string text = "Resource not found...";
    using (var sr = new StreamReader(DataAccess.GetStream("flow.txt")))
    {
        text = sr.ReadToEnd();
    }
    text = text.Replace("\t", "   ");
    
    // create pdf document
    word.Info.Title = "Text Flow";
    word.LineBreak();
    
    // add title
    Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);
    Font bodyFont = new Font("Tahoma", 9);
    Rect rcPage = WordUtils.PageRectangle(word);
    var paragraph = new RtfParagraph();
    var title = new RtfString(word.Info.Title, titleFont, RtfUnderlineStyle.Dotted);
    paragraph.Add(title);
    word.Add(paragraph);
    word.LineBreak();
    word.LineBreak();
                    
    // render string spanning columns and pages
    foreach (var s in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
    {
        word.AddParagraph(s, bodyFont, Colors.Black, RtfHorizontalAlignment.Justify);
    }
                                    
    

    The output of the above code will look similar to the image given below: