Thursday, 06 August 2015 06:00

How to set position and custom style for page borders

Written by  support iceblue
Rate this item
(0 votes)

MS page borders options provide the choice to set the position for page border by margins measured from text or edge of page. Besides, it provides the option for custom page border by setting the style and color of top, bottom, left, right borders. We have introduced the method to set page borders and set whether page border surrounds header/footer or not using Spire.Doc. This article is going to introduce the solution provided by Spire.Doc to set the position and custom style for page borders in C#.

Note: before start, please download the latest version of Spire.Doc and add the .dll in the bin folder as the reference of Visual Studio.

Step 1: Load the sample document with no page border.

            Document document = new Document();
            document.LoadFromFile("S.docx");
            Section section = document.Sections[0];

Step 2: Set the position of page borders and its space. Effects will show the difference between page border measured from text and page edge with the same space.

            section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
            section.PageSetup.Borders.Top.Space = 20;
            section.PageSetup.Borders.Bottom.Space = 30;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space =25;

Step 3: Set the style and color of the top, bottom, left, right page borders, namely custom page borders.

            section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
            section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

            section.PageSetup.Borders.Top.Color = Color.YellowGreen;
            section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

Step 4: Save the document and launch to see effects.

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 

Effects:

Page border position measured from text:

How to set position and custom style for page borders

Page border position measured from page edge:

How to set position and custom style for page borders

Full codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Mirror_Margin
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("S.docx");
            Section section = document.Sections[0];

            section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
            section.PageSetup.Borders.Top.Space = 20;
            section.PageSetup.Borders.Bottom.Space = 30;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space =25;
            
            section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
            section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

            section.PageSetup.Borders.Top.Color = Color.YellowGreen;
            section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 
        }
    }
}

Additional Info

  • tutorial_title: Set position and custom style for page borders