Document Solutions for Word
Features / Table / Table
In This Topic
    Table
    In This Topic

    DsWord allows you to add, modify, and delete a table from a Word document. A table can be created in a document using Add or Insert method of the TableCollection class. It also lets you format the table using the TableFormat class properties and apply styles to the table using Style class properties.

    Table in a Word document

    Create Table

    To create a table in Word document using DsWord:

    1. Create a new Word document using an instance of the GcWordDocument class.
    2. Get range of the first section in body of the document and add an empty table to the first section using the Add method.
    3. Add rows and cells to the table using the Add method.
    4. Create a new table style using the Style class and apply it on the table.
    5. Save the document using the Save method.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      //Access the first section of the document
      Section section = doc.Body.Sections.First;
      //Add a paragraph in the section
      Paragraph header = section.GetRange().Paragraphs.Add(" Adding a table:");
      
      // Add an empty table with:
      int rows = 6;
      int cols = 4;
      Table t = section.GetRange().Tables.Add(0, 0);
      
      // Add some rows and cells to it:
      List<string> cells = new List<string>(cols);
      for (int col = 0; col < cols; ++col)
          cells.Add(string.Empty);
      for (int row = 0; row < rows; ++row)
      {
          for (int col = 0; col < cols; ++col)
              cells[col] = $"Row {row + 1}, col {col + 1}";
          t.Rows.Add(cells.ToArray());
      }
      
      // Create a new table style:
      Style ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
      foreach (var border in ts1.Table.Borders)
      {
          border.LineStyle = LineStyle.BasicBlackDots;
          border.LineWidth = 0.5f;
          border.Color.RGB = Color.Purple;
      }
      
      //Apply the table style on the table
      t.Style = ts1;
                  
      //Save the document
      doc.Save("CreateTable.docx");
      Back to Top

    Modify Table

    To modify the table formatting in a Word document:

    1. Load the document created in Create Table section, using the Load method.
    2. Access formatting properties of the table through Format property of the Table class.
    3. Modify the table. For example, modify the alignment of the table using the Alignment property and set the AllowAutoFit property to allow the table cells to automatically resize according to their content.
      C#
      Copy Code
      //Load an existing document
      doc.Load("CreateTable.docx");
      
      //Access the table
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      
      //Modify the table
      t.Format.AllowAutoFit = true;
      t.Format.Alignment = TableAlignment.Center;
      
      //Save the document
      doc.Save("ModifiedDoc.docx");
    Back to Top

    Delete Table

    To delete a table from a Word document:

    1. Access the table from the table collection using Tables property of the RangeBase class.
    2. Delete the table using the Delete method.
      C#
      Copy Code
      //Load an existing document
      doc.Load("CreateTable.docx");
      
      //Access the table
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      
      //Delete the table
      t.Delete();
      
      //Save the document
      doc.Save("TableDeleted.docx");
    Back to Top

    AutoFit Table

    To allow automatic resizing of cells in a table according to their content, use AllowAutoFit property of the TableFormatBase class.

    C#
    Copy Code
    //Load an existing document
    doc.Load("CreateTable.docx");
    
    //Access the table and allow automatic resizing of the cells
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    t.Format.AllowAutoFit = true;
    
    //Save the document
    doc.Save("AutoFitEnabled.docx");
    Back to Top

    Split Table

    To spit a table in a Word document:

    1. Access a table and then access its row. For example, access the second row of the table.
    2. Split the table from the specified row of an existing table.
    3. Insert a paragraph between the two tables. This enables you to view two separate tables.
      C#
      Copy Code
      doc.Load("CreateTable.docx");
      
      //Access the table and it's second row
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      Row row = t.Rows[1];
      
      //This generates a new table from the specified row of an existing table.
      Table splitTable1 = t.Split(row, InsertLocation.After);
      
      //Insert a paragraph (without content) between the two tables
      splitTable1.GetRange().Paragraphs.Insert(InsertLocation.Before);
      
      //Save the document
      doc.Save("SplitTable.docx");
    Back to Top

    Set Table Styles

    To set table styles in a Word document:

    1. Access the table from the table collection using Tables property of the RangeBase class.
    2. Add a new table style to the style collection using Add method of StyleCollection class.
    3. Apply the style on the table using Style property of Table class.
    4. Set formatting properties of the table, such as Description, Alignment, and Title and save the document.
      C#
      Copy Code
      //Load the document
      doc.Load("CreateTable.docx");
      
      //Access the table
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
                  
      // Create a new table style:
      Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
      ts1.Table.ColumnStripe=3;
      
      // Assign the style to the table:
      t.Style = ts1;
      
      //Set table formatting
      t.Format.Alignment = TableAlignment.Center;
      t.Format.Title = "Test Table";
      t.Format.Description = "This is the table description";
      
      //Save the document
      doc.Save("SetTableInfo.docx");
    Back to Top

    Get Table Styles

    To get table styles and formatting:

    1. Access the table from the table collection using Tables property of the RangeBase class.
    2. Fetch the existing table styles using the Style property and the table formatting using the Format property.
    3. Display the fetched details on the console.
      C#
      Copy Code
      //Load the document
      doc.Load("SetTableInfo.docx");
      
      //Access the table
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      
      //Get table styles
      Style st=t.Style;
      uint cstripe = st.Table.ColumnStripe;   
      Console.WriteLine("Table stripe: " + cstripe.ToString());
      
      //Get table formatting
      TableFormat tformat = t.Format;
      string title = tformat.Title;
      
      //Write the fetched title of the table on the console
      Console.WriteLine("Table Title: " + title);
    Back to Top

    Set Indents

    To set the indentation of a table in Word document:

    1. Add a new table style to the style collection using the Add method.
    2. Set the indentation of the table, in points, using the Indent property
    3. Apply the style on the table using the Style property.
      C#
      Copy Code
      //Load the table
      doc.Load("CreateTable.docx");
      
      //Access the table and allow automatic resizing of the cells
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      t.Format.AllowAutoFit = true;
      
      // Create a new table style to set the Indentation
      Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
      
      //Set the indentation
      ts1.Table.Indent = 3;
      
      //Apply the style
      t.Style = ts1;
      
      //Save the document
      doc.Save("SetIndent.docx");
    Back to Top

    Get Indents

    To get the indentation of a table from a Word document:

    1. Access the table from the table collection using Tables property of the RangeBase class.
    2. Fetch indentation of the table using Indent property.
    3. Display the fetched table indentation on the console.
      C#
      Copy Code
      //Load an existing document
      doc.Load("SetIndent.docx");
      
      //Access the table
      Table t = doc.Body.Sections.First.GetRange().Tables[0];
      
      //Get the style applied on the table
      Style s = t.Style;
      
      //Get the indentation
      float indenting=s.Table.Indent;  
                  
      //Write the fetched indentation(in points) on the console
      Console.WriteLine("Get Indenting: " + indenting);
    Back to Top

    For more information about implementation of tables using DsWord, see DsWord sample browser.