ComponentOne Word for UWP
Working with Word for UWP / 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 UWP, 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\UWP\WordSample
    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 UWP:

     ' load long string from resource file
    Dim text As String = Strings.ResourceNotFound
    
    Using sr = New StreamReader(GetType(BasicTextPage).GetTypeInfo().
              Assembly.GetManifestResourceStream("WordSamples.Resources.flow.txt"))
            text = sr.ReadToEnd()
    End Using
    text = text.Replace(vbTab, "   ")
    
    ' create word document
    word.Info.Title = "Text Flow"
    
    ' table
    Dim rows As Integer = 4
    Dim cols As Integer = 2
    Dim table As New RtfTable(rows, cols)
    word.Add(table)
    table.Rows(0).Cells(0).SetMerged(1, 2)
    
    For row As Integer = 0 To rows - 1
            If row = 0 Then
                    table.Rows(row).Height = 50
            End If
            For col As Integer = 0 To cols - 1
                    'RtfParagraph paragraph = new RtfParagraph();
                    'paragraph.Alignment = RtfHorizontalAlignment.Undefined;
                    'paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
                    'table.Rows[row].Cells[col].Content.Add(paragraph);
                    table.Rows(row).Cells(col).Content.Add(New RtfString(String.Format("table cell {0}:{1}.", row, col)))
                    If row = 0 AndAlso col = 0 Then
                            table.Rows(row).Cells(col).Alignment = ContentAlignment.MiddleCenter
                            table.Rows(row).Cells(col).BackFilling = Colors.LightPink
                    Else
                            table.Rows(row).Cells(col).BackFilling = Colors.LightYellow
                    End If
                    table.Rows(row).Cells(col).BottomBorderWidth = 2
                    table.Rows(row).Cells(col).TopBorderWidth = 2
                    table.Rows(row).Cells(col).LeftBorderWidth = 2
                    table.Rows(row).Cells(col).RightBorderWidth = 2
            Next
    Next
    
    Return
    
    ' 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 rc As Rect = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rcPage, False)
    rc.Y += titleFont.Size + 6
    rc.Height = rcPage.Height - rc.Y
    
    ' create two columns for the text
    Dim rcLeft As Rect = rc
    rcLeft.Width = rcPage.Width / 2 - 12
    rcLeft.Height = 300
    rcLeft.Y = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2
    Dim rcRight As Rect = rcLeft
    rcRight.X = rcPage.Right - rcRight.Width
    
    ' start with left column
    rc = rcLeft
    
    ' render string spanning columns and pages
    While True
            ' render as much as will fit into the rectangle
            rc = WordUtils.Inflate(rc, -3, -3)
            'int nextChar = word.DrawString(text, bodyFont, Colors.Black, rc);
            word.DrawString(text, bodyFont, Colors.Black, rc)
            rc = WordUtils.Inflate(rc, +3, +3)
            word.DrawRectangle(Colors.LightGray, rc)
    
            ' break when done
            'if (nextChar >= text.Length)
            If True Then
                    Exit While
            End If
    
            ' get rid of the part that was rendered
            'text = text.Substring(nextChar);
    
            ' switch to right-side rectangle
            If rc.Left = rcLeft.Left Then
                    rc = rcRight
            Else
                    ' switch to left-side rectangle on the next page
                    word.PageBreak()
                    rc = rcLeft
            End If
    End While
    
    // load long string from resource file
    string text = Strings.ResourceNotFound;
    
    using(var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().
                   Assembly.GetManifestResourceStream("WordSamples.Resources.flow.txt"))) {
      text = sr.ReadToEnd();
    }
    text = text.Replace("\t", "   ");
    
    // create word document
    word.Info.Title = "Text Flow";
    
    // table
    int rows = 4;
    int cols = 2;
    RtfTable table = new RtfTable(rows, cols);
    word.Add(table);
    table.Rows[0].Cells[0].SetMerged(1, 2);
    
    for (int row = 0; row < rows; row++) {
      if (row == 0) {
        table.Rows[row].Height = 50;
      }
      for (int col = 0; col < cols; col++) {
        //RtfParagraph paragraph = new RtfParagraph();
        //paragraph.Alignment = RtfHorizontalAlignment.Undefined;
        //paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
        //table.Rows[row].Cells[col].Content.Add(paragraph);
        table.Rows[row].Cells[col].Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
        if (row == 0 && col == 0) {
          table.Rows[row].Cells[col].Alignment = ContentAlignment.MiddleCenter;
          table.Rows[row].Cells[col].BackFilling = Colors.LightPink;
        } else {
          table.Rows[row].Cells[col].BackFilling = Colors.LightYellow;
        }
        table.Rows[row].Cells[col].BottomBorderWidth = 2;
        table.Rows[row].Cells[col].TopBorderWidth = 2;
        table.Rows[row].Cells[col].LeftBorderWidth = 2;
        table.Rows[row].Cells[col].RightBorderWidth = 2;
      }
    }
    
    return;
    
    // add title
    Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);
    Font bodyFont = new Font("Tahoma", 9);
    Rect rcPage = WordUtils.PageRectangle(word);
    Rect rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rcPage, false);
    rc.Y += titleFont.Size + 6;
    rc.Height = rcPage.Height - rc.Y;
    
    // create two columns for the text
    Rect rcLeft = rc;
    rcLeft.Width = rcPage.Width / 2 - 12;
    rcLeft.Height = 300;
    rcLeft.Y = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2;
    Rect rcRight = rcLeft;
    rcRight.X = rcPage.Right - rcRight.Width;
    
    // start with left column
    rc = rcLeft;
    
    // render string spanning columns and pages
    for (;;) {
      // render as much as will fit into the rectangle
      rc = WordUtils.Inflate(rc, -3, -3);
      //int nextChar = word.DrawString(text, bodyFont, Colors.Black, rc);
      word.DrawString(text, bodyFont, Colors.Black, rc);
      rc = WordUtils.Inflate(rc, +3, +3);
      word.DrawRectangle(Colors.LightGray, rc);
    
      // break when done
      //if (nextChar >= text.Length)
      {
        break;
      }
    
      // get rid of the part that was rendered
      //text = text.Substring(nextChar);
    
      // switch to right-side rectangle
      if (rc.Left == rcLeft.Left) {
        rc = rcRight;
      } else // switch to left-side rectangle on the next page
      {
        word.PageBreak();
        rc = rcLeft;
      }
    }
    

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