C#: Add, Modify, or Remove Word Table Borders

2024-06-20 01:23:39 Written by  support iceblue
Rate this item
(0 votes)

Adding, modifying, and removing Word table borders can enhance the readability, aesthetics, and organization of data. Adding borders makes the content of the table clearer, distinguishing between different cells, which helps readers quickly identify information. Modifying border styles (such as line thickness, color, or pattern) can emphasize key data, guide visual flow, or conform to specific document styles and design requirements. Removing borders, in some cases, reduces visual clutter, making the content more compact and minimalist, especially suitable for data presentation where strict divisions are not needed or when you wish to downplay structural visibility. This article will introduce how to add, modify, or remove Word table borders in C# projects using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

C# Add Word Table Borders

To set borders for all cells in an entire Word table, you need to iterate over each cell and set its visual border properties. Here are the detailed steps:

  • Create a Document object.
  • Use the Document.LoadFromFile() method to load a document.
  • Retrieve the first section of the document using Document.Sections[0].
  • Get the first table in that section by using Section.Tables[0].
  • Use a for loop to iterate through all the cells in the table.
  • Set TableCell.CellFormat.Borders.BorderType to BorderStyle.Single, which sets the cell border to a single line style.
  • Set TableCell.CellFormat.Borders.LineWidth to 1.5, defining the border width to be 1.5 points.
  • Set TableCell.CellFormat.Borders.Color to Color.Black, setting the border color to black.
  • Save the changes to the Word document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new Document object
            Document doc = new Document();

            // Load the document from a file
            doc.LoadFromFile("TableExample1.docx");

            // Get the first section of the document
            Section section = doc.Sections[0];

            // Get the first table in that section
            Table table = (Table)section.Tables[0];

            // Declare TableRow and TableCell variables for use within loops
            TableRow tableRow;
            TableCell tableCell;

            // Iterate through all rows in the table
            for (int i = 0; i < table.Rows.Count; i++)
            {
                // Get the current row
                tableRow = table.Rows[i];

                // Iterate through all cells in the current row
                for (int j = 0; j < tableRow.Cells.Count; j++)
                {
                    // Get the current cell
                    tableCell = tableRow.Cells[j];

                    // Set the border style of the current cell to single line
                    tableCell.CellFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                }
            }

            // Save the modified document as a new file
            doc.SaveToFile("AddBorders.docx", FileFormat.Docx2016);

            // Close the document to release resources
            doc.Close();
        }
    }
}

C#: Add, Modify, or Remove Word Table Borders

C# Modify Word Table Borders

Spire.Doc offers a range of border properties such as the border style TableCell.CellFormat.Borders.BorderType, border width TableCell.CellFormat.Borders.LineWidth, and border color TableCell.CellFormat.Borders.Color, among others. You can customize these properties to achieve the desired effects. Below are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.LoadFromFile() method.
  • Retrieve the first section of the document using Document.Sections[0].
  • Get the first table in the section using Section.Tables[0].
  • Use a for loop to iterate over the cells in the table whose border styles you wish to change.
  • Change the bottom border color of the cell by setting TableCell.CellFormat.Borders.Bottom.Color to Color.PaleVioletRed.
  • Change the bottom border style of the cell by setting TableCell.CellFormat.Borders.Bottom.BorderType to BorderStyle.DotDash.
  • Change the bottom border width of the cell by setting TableCell.CellFormat.Borders.Bottom.LineWidth to 2 points.
  • Save the changes to the document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new Document object
            Document doc = new Document();

            // Load the document from a file
            doc.LoadFromFile("TableExample2.docx");

            // Get the first section of the document
            Section section = doc.Sections[0];

            // Get the first table in that section
            Table table = (Table)section.Tables[0];

            // Declare a TableRow to use within the loop
            TableRow tableRow;

            // Iterate through all rows of the table
            for (int i = 1; i < table.Rows.Count - 1; i++)
            {
                tableRow = table.Rows[i];

                // Set the border color of the current cell
                tableRow.Cells[1].CellFormat.Borders.Bottom.Color = Color.PaleVioletRed;

                // Set the border style of the current cell to DotDash
                tableRow.Cells[1].CellFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.DotDash;

                // Set the width of the border
                tableRow.Cells[1].CellFormat.Borders.Bottom.LineWidth = 2;
            }

            // Save the modified document as a new file
            doc.SaveToFile("ModifiedBorders.docx", FileFormat.Docx2016);

            // Close the document and release resources
            doc.Close();
        }
    }
}

C#: Add, Modify, or Remove Word Table Borders

C# Remove Word Table Borders

During the process of handling Word documents, not only can border styles be applied to entire tables, but customization can also be extended to individual cells. To completely remove all borders from a table, it is recommended to follow a two-step strategy: First, apply border removal settings to the table itself; second, visit each cell within the table individually to clear their border styles. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.LoadFromFile() method.
  • Retrieve the first table in the section using Section.Tables[0].
  • Use a for loop to iterate over all cells in the table.
  • Set Table.TableFormat.Borders.BorderType = BorderStyle.None to remove borders from the table.
  • Set TableCell.CellFormat.Borders.BorderType = BorderStyle.None to remove borders from each cell.
  • Save the changes to the Word document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new Document object
            Document doc = new Document();

            // Load the document from file
            doc.LoadFromFile("TableExample2.docx");

            // Get the first section of the document
            Section section = doc.Sections[0];

            // Get the first table in that section
            Table table = (Table)section.Tables[0];

            // Remove the borders set on the table
            table.TableFormat.Borders.BorderType = BorderStyle.None;

            // Declare a TableRow to use in the loop
            TableRow tableRow;

            // Iterate through all rows in the table
            for (int i = 0; i < table.Rows.Count; i++)
            {
                tableRow = table.Rows[i];
                for (int j = 0; j < tableRow.Cells.Count; j++)
                {
                    // Remove all borders set on the cell
                    tableRow.Cells[j].CellFormat.Borders.BorderType = BorderStyle.None;
                }
            }

            // Save the modified document as a new file
            doc.SaveToFile("RemoveBorders.docx", FileFormat.Docx2016);

            // Close the document to release resources
            doc.Close();
        }
    }
}

C#: Add, Modify, or Remove Word Table Borders

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.