Set up page borders in word in WPF

2016-03-18 01:14:32 Written by  support iceblue
Rate this item
(0 votes)

A page border is a border that appears outside the margins on each page. Page borders are primarily for decoration so you can use any style, color, and line thickness you want. A good page border can make your page more appealing. In this section, we will demonstrate how to set up page borders in word using Spire.Doc for WPF.

Step 1: Initialize a new instance of Document class. Load the word document from the file.

Document document = new Document();
document.LoadFromFile("Emily.docx");

Step 2: Add page borders and set up the format of borders. In this instance, we set the style of border to ThinThickLargeGap and set the color to yellow green.

Section section = document.Sections[0];
section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
section.PageSetup.Borders.Color = Color.YellowGreen;

Step 3: Set up the space between the borders and the text.

section.PageSetup.Borders.Left.Space = 50;
section.PageSetup.Borders.Right.Space = 50;
section.PageSetup.Borders.Top.Space = 50;
section.PageSetup.Borders.Bottom.Space = 50;

Step 4: Save the document and launch the file.

document.SaveToFile("Dickinson.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Dickinson.docx");

Effective Screenshots:

Set up page borders in word in WPF

Full codes:

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

namespace Emily
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile("Emily.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
            section.PageSetup.Borders.Color = Color.YellowGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;
            section.PageSetup.Borders.Top.Space = 50;
            section.PageSetup.Borders.Bottom.Space = 50;
            //Save and Launch
            document.SaveToFile("Dickinson.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Dickinson.docx");
        }
    }
        
}
[VB.NET]
Imports System.Drawing
Imports System.Windows
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Emily
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Document
			Dim document As New Document()
			document.LoadFromFile("Emily.docx")
			Dim section As Section = document.Sections(0)

			'Add Page Borders with Special Style and Color
			section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap
			section.PageSetup.Borders.Color = Color.YellowGreen
			'Space between Border and Text
			section.PageSetup.Borders.Left.Space = 50
			section.PageSetup.Borders.Right.Space = 50
			section.PageSetup.Borders.Top.Space = 50
			section.PageSetup.Borders.Bottom.Space = 50
			'Save and Launch
			document.SaveToFile("Dickinson.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("Dickinson.docx")
		End Sub
	End Class

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:51