ComponentOne Word for UWP
Working with Word for UWP / Advanced Level Working / Inserting Table
In This Topic
    Inserting Table
    In This Topic

    Table is used in word documents to present data in a well formatted form with the help of rows and columns. Word for UWP enables you to add table to a word document using the following code:

    ' add table
    word.LineBreak()
    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
                    If row = 0 AndAlso col = 0 Then
                            text = Strings.DocumentBasicText2
                            table.Rows(row).Cells(col).Alignment = ContentAlignment.MiddleCenter
                            table.Rows(row).Cells(col).BackFilling = Colors.LightPink
                    Else
                            text = String.Format("table cell {0}:{1}.", row, col)
                            table.Rows(row).Cells(col).BackFilling = Colors.LightYellow
                    End If
                    table.Rows(row).Cells(col).Content.Add(New RtfString(text))
                    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
                    If col = cols - 1 Then
                            table.Rows(row).Cells(col).Alignment = ContentAlignment.BottomRight
                    End If
            Next
    Next
    
    // add table
    word.LineBreak();
    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++) {
        if (row == 0 && col == 0) {
          text = Strings.DocumentBasicText2;
          table.Rows[row].Cells[col].Alignment = ContentAlignment.MiddleCenter;
          table.Rows[row].Cells[col].BackFilling = Colors.LightPink;
        } else {
          text = string.Format("table cell {0}:{1}.", row, col);
          table.Rows[row].Cells[col].BackFilling = Colors.LightYellow;
        }
        table.Rows[row].Cells[col].Content.Add(new RtfString(text));
        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;
        if (col == cols - 1) {
          table.Rows[row].Cells[col].Alignment = ContentAlignment.BottomRight;
        }
      }
    }
    

    The above code creates a table with proper indentation and alignment in a word document.

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