News Category

C#/VB.NET: Set Borders for Word Tables or Cells

Microsoft Word offers a wide range of formatting options to customize tables, and one of the most valuable options is setting borders. By applying borders, you can create a polished and professional-looking table that enhances readability and visual impact. In this article, you will learn how to set borders for a table or specific table cells in a Word document in C# and VB.NET 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

Set Borders for Word Tables or Cells using C# and VB.NET

With Spire.Doc for .NET, you can set different types of borders, such as top borders, bottom borders, left borders, right borders, inside borders and diagonal borders to a Word table. Besides, you are also able to apply borders to specific table cells. The main steps are as follows:

  • Initialize an instance of the Document class.
  • Add a section to the document using Document.AddSection() method.
  • Add a table to the section using Section.AddTable() method, then set the number of rows and columns of the table using Table.ResetCells() method.
  • Get the borders collection of the table using Table.TableFormat.Borders property.
  • Access the specific borders, such as top borders, bottom borders, left borders, right borders from the borders collection using Borders.Top, Borders.Bottom, Borders.Left, Borders.Right properties, then set the line style, width and color for the specific borders.
  • Get a specific table cell using Table[int row, int column] property (note: the row or column index starts from 0).
  • Get the borders collection of the cell using TableCell.CellFormat.Borders property.
  • Access the diagonal-down border from the borders collection using Borders.DiagonalDown property, then set the line style, width and color for the border.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Formatting;
using System.Drawing;

namespace SetTableBorders
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();

            //Add a section to the document
            Section section = document.AddSection();
            section.PageSetup.Margins.All = 72f;

            //Add a table to the section
            Table table = section.AddTable();
            //Set the number of rows and columns in the table
            table.ResetCells(5, 3);

            //Get the borders collection of the table
            Borders borders = table.TableFormat.Borders;

            //Get the top borders and set border style, line width and color
            Border topBorder = borders.Top;
            topBorder.BorderType = BorderStyle.Double;
            topBorder.LineWidth = 1.0f;
            topBorder.Color = Color.YellowGreen;

            //Get the left borders and set border style, line width and color
            Border leftBorder = borders.Left;
            leftBorder.BorderType = BorderStyle.Double;
            leftBorder.LineWidth = 1.0f;
            leftBorder.Color = Color.YellowGreen;

            //Get the right borders and set border style, line width and color
            Border rightBorder = borders.Right;
            rightBorder.BorderType = BorderStyle.Double;
            rightBorder.LineWidth = 1.0f;
            rightBorder.Color = Color.YellowGreen;

            //Get the bottom borders and set border style, line width and color
            Border bottomBorder = borders.Bottom;
            bottomBorder.BorderType = BorderStyle.Double;
            bottomBorder.LineWidth = 1.0f;
            bottomBorder.Color = Color.YellowGreen;

            //Get the inside horizontal borders and set border style, line width and color
            Border horizontalBorder = borders.Horizontal;
            horizontalBorder.BorderType = BorderStyle.Hairline;
            horizontalBorder.LineWidth = 1.0f;
            horizontalBorder.Color = Color.Orange;

            //Get the inside vertical borders and set border style, line width and color
            Border verticalBorder = borders.Vertical;
            verticalBorder.BorderType = BorderStyle.Dot;
            verticalBorder.LineWidth = 1.0f;
            verticalBorder.Color = Color.CornflowerBlue;

            //Get the first table cell
            TableCell cell = table[0, 0];
            //Get the borders collection of the cell
            Borders cellBorders = cell.CellFormat.Borders;
            //Get the diagonal down border and set border style, line width and color
            Border diagonalDownBorder = cellBorders.DiagonalDown;
            diagonalDownBorder.BorderType = BorderStyle.DotDash;
            diagonalDownBorder.LineWidth = 1.0f;
            diagonalDownBorder.Color = Color.Purple;

            //Save the result document
            document.SaveToFile("SetTableBorders.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Set Borders for Word Tables or Cells

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.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 09 August 2023 00:45