C#: Add, Modify, or Remove Page Borders in Word

Page borders in Microsoft Word can be a useful tool for improving the visual appeal and organization of your documents. Knowing how to effectively add, modify, and remove page borders can help you customize the appearance of your Word documents and elevate their overall presentation.

In this article, you will learn how to programmatically manage page borders in Word documents using C# and Spire.Doc for .NET.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Add Borders to All Pages in Word in C#

Spire.Doc for .NET provides the Borders class to manage the page borders in a Word document. This class offers a set of properties that allow you to control the appearance of the page border, including BorderType, Color, and LineWidth.

The steps to add borders to all pages in a Word document using C# are as follows:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Iterate through the sections in the document.
    • Get a specific section.
    • Get the PageSetup object of the section.
    • Apply borders to all page by setting PageSetup.PageBordersApplyType to PageBordersApplyType.AllPages.
    • Get the Borders object through PageSetup.Borders property.
    • Set the border type, color, line width and other attributes through the properties under the Borders object.
  • Save the updated document to a different Word file.
  • C#
using Spire.Doc;
using System.Drawing;

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

            // Load a Word file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");

            // Iterate through the sections in the document
            for (int i = 0; i < doc.Sections.Count; i++)
            {
                // Get a specific section
                Section section = doc.Sections[i];

                // Get page setup object
                PageSetup pageSetup = section.PageSetup;

                // Apply page border to all pages
                pageSetup.PageBordersApplyType = PageBordersApplyType.AllPages;

                // Set the border type
                pageSetup.Borders.BorderType = Spire.Doc.Documents.BorderStyle.DashLargeGap;

                // Set the border width
                pageSetup.Borders.LineWidth = 2;

                // Set the border color
                pageSetup.Borders.Color = Color.Red;

                // Set the spacing between borders and text within them
                pageSetup.Borders.Top.Space = 30;
                pageSetup.Borders.Bottom.Space = 30;
                pageSetup.Borders.Left.Space = 30;
                pageSetup.Borders.Right.Space = 30;
            }

            // Save the updated document to a different file
            doc.SaveToFile("AddPageBorder.docx", FileFormat.Docx);

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add, Modify, or Remove Page Borders in Word

Modify Page Borders in Word in C#

The page borders in an existing Word document can be accessed through PageSetup.Borders class and their appearance can be modified using the properties under the Borders object.

The following are the steps to modify page border in a Word document using C#:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Iterate through the sections in the document.
    • Get a specific section.
    • Get the PageSetup object of the section.
    • Get the Borders object through PageSetup.Borders property.
    • Set the border type, color, line width and other attributes through the properties under the Borders object.
  • Save the updated document to a different Word file.
  • C#
using Spire.Doc;
using System.Drawing;

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

            // Load a Word file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Border.docx");

            // Iterate through the sections in the document
            for (int i = 0; i < doc.Sections.Count; i++)
            {
                // Get a specific section
                Section section = doc.Sections[i];

                // Get page setup of the section
                PageSetup pageSetup = section.PageSetup;

                // Change the border type
                section.PageSetup.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Double;

                // Change the border color
                section.PageSetup.Borders.Color = Color.Orange;

                // Change the border width
                section.PageSetup.Borders.LineWidth = 3;
            }

            // Save the updated document to a different file
            doc.SaveToFile("ModifyBorder.docx", FileFormat.Docx);

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add, Modify, or Remove Page Borders in Word

Remove Page Borders in Word in C#

To remove the page borders of a Word document, you can simply set the Borders.BorderType property to BorderStyle.None. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file from the given file path.
  • Iterate through the sections in the document.
    • Get a specific section.
    • Get the PageSetup object of the section.
    • Get the Borders object through PageSetup.Borders property.
    • Set the Borders.BorderType property as BorderStyle.None.
  • Save the updated document to a different Word file.
  • C#
using Spire.Doc;

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

            // Load a Word file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Border.docx");

            // Iterate through the sections in the document
            for (int i = 0; i < doc.Sections.Count; i++)
            {
                // Get a specific section
                Section section = doc.Sections[i];

                // Get page setup object
                PageSetup pageSetup = section.PageSetup;

                // Set the border type to none
                pageSetup.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
            }

            // Save the updated document to a different file
            doc.SaveToFile("RemovePageBorder.docx", FileFormat.Docx);

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add, Modify, or Remove Page Borders in Word

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.