News Category

Add Page Borders for Word in C#, VB.NET

2012-05-04 02:16:02 Written by  support iceblue
Rate this item
(0 votes)

Word Page Border is one part of page background to beautify document appearance. Solution in this guide introduces how to insert and format Word page border in C# and VB.NET.

MS Word enables users to decorate Word pages to have a better layout or appearance. All the formats of page borders can be customized, including style, color, width, margin etc. This guide demonstrates how to add page borders for Word document and format them in C# and VB.NET via Spire.Doc for .NET. The following screenshot presents Word with page borders after programming.

Word Page Borders

Spire.Doc for .NET offers Section class to allow you to create new section instance or manipulate existing section. Borders is one property of PageSetup of Section class. You can directly set BorderType property of Borders to add page borders and set Color, Space of Left/Right properties to format borders. The border types Spire.Doc for .NET offers include None, Single, Thick, Double, Hairline, Dot, DashLargeGap, DotDash, DotDotDash, Triple, ThinkThickSmallGap, ThinThinSampllGap, ThinThickThinSamllGap, ThinThickMediumGap, ThickThinMediumGap, ThickThickThinMediumGap, ThinThickLargeGap, ThickThinLargeGap, ThinThickThinLargeGap, Wave, DoubleWave, DashSamllGap, DashDotStroker, Emboss3D, Engrave3D, Outset, Inset, TwistedLines1, Cleared. In this example, the border type is set as DoubleWave.

Download and install Spire.Doc for .NET and follow the code:

[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordBorder
{
    class PageBorders
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Microsoft Word 2013 Preview.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave;
            section.PageSetup.Borders.Color = Color.LightSeaGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;

            //Save and Launch
            document.SaveToFile("PageBorders.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("PageBorders.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBorder
    Friend Class PageBorders
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Microsoft Word 2013 Preview.docx")
            Dim section As Section = document.Sections(0)

            'Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave
            section.PageSetup.Borders.Color = Color.LightSeaGreen
            'Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50
            section.PageSetup.Borders.Right.Space = 50

            'Save and Launch
            document.SaveToFile("PageBorders.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("PageBorders.docx")
        End Sub
    End Class
End Namespace

Spire.Doc is a stand-alone component, enabling developers/programmers to generate, open, write, edit and save Word document in WPF, .NET and Silverlight without Microsoft Word installed on system.

Additional Info

  • tutorial_title: Add Page Borders
Last modified on Friday, 03 September 2021 03:38