Page margins are the distance from the page edges to the content area. They are helpful in making a document look neat and professional. When generating a document in Microsoft Word, you may need to set page margins to make the document look better. In this article, we will demonstrate how to set page margins for Word documents 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 Page Margins in Word in C# and VB.NET

The following are the steps to set page margins in Word:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the desired section through Document.Sections[sectionIndex] property.
  • Set the top, bottom, left and right margins for the pages in the section through Section.PageSetup.Margins.Top, Section.PageSetup.Margins.Bottom, Section.PageSetup.Margins.Left, Section.PageSetup.Margins.Right properties.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace PageMargins
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Set top, bottom, left and right page margins for the section
            section.PageSetup.Margins.Top = 17.9f;
            section.PageSetup.Margins.Bottom = 17.9f;
            section.PageSetup.Margins.Left = 17.9f;
            section.PageSetup.Margins.Right = 17.9f;

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

C#/VB.NET: Set Page Margins for Word Documents

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.

Published in Page Setup
Thursday, 26 April 2012 06:44

Remove Word Section in C#, VB.NET

In Word document, users can add new sections or remove existing sections. This guide demonstrates how to remove Word section in C# and VB.NET.

Section, important part to form a Word document, can be easily added and removed. In MS Word, users can operation section by section break, adding new section by inserting a break and deleting existing section by removing section break. In this guide, a solution will be presented to remove Word section in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET enables developers to access SectionCollection property of Document class to get specified section in Word document and then remove the unnecessary one or more. There are two methods to remove Word section.

Remove one Specified Section

Invoke Remove(Spire.Doc.Interface.IDocumentObject entity) method to remove specified object of document. In this solution, entity is the first section of document which has gotten at first.

[C#]
            Section section = document.Sections[0];
            document.Sections.Remove(section);
[VB.NET]
            Dim section As Section = document.Sections(0)
            document.Sections.Remove(section)

Invoke RemoveAt(int index) method to remove document object at the specified index from the collection.

[C#]
            document.Sections.RemoveAt(0);
[VB.NET]
	    document.Sections.RemoveAt(0)

Remove All Sections

Invoke Clear() method to clear all objects of document.

[C#]
            document.Sections.Clear();
[VB.NET]
            document.Sections.Clear()

Note: Because a Word document must include at least one section, so the document cannot be saved if all the sections are removed.

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.

Published in Page Setup

A page break is a marker that controls where one page ends and where a new page begins. If you want to move the content after a certain place to the next page in your Word document, you can insert a page break. In this article, you will learn how to insert page break into Word documents in C# and VB.NET using Spire.Doc for .NET library.

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

Insert Page Break after a Specific Paragraph

The following are the steps to insert page break after a specific paragraph:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.Sections[sectionIndex] property.
  • Get the desired paragraph using Section.Paragraphs[paragraphIndex] property.
  • Add a page break to the paragraph using Paragraph.AppendBreak(BreakType.PageBreak) method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertPageBreakAfterParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];
            //Get the 2nd paragraph in the section
            Paragraph paragraph = section.Paragraphs[1];

            //Append a page break to the paragraph
            paragraph.AppendBreak(BreakType.PageBreak);

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

C#/VB.NET: Insert Page Break into Word Documents

Insert Page Break after a Specific Text

The following are the steps to insert a page break after a specific text:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Find a specific text using Document.FindString() method.
  • Access the text range of the searched text using TextSelection.GetAsOneRange() method.
  • Get the paragraph where the text range is located using ParagraphBase.OwnerParagraph property.
  • Get the position index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Initialize an instance of Break class to create a page break.
  • Insert the page break after the searched text using Paragraph.ChildObjects.Insert() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace InsertPageBreakAfterText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Search a specific text 
            TextSelection selection = document.FindString("celebration", true, true);
            //Get the text range of the seached text
            TextRange range = selection.GetAsOneRange();
            //Get the paragraph where the text range is located
            Paragraph paragraph = range.OwnerParagraph;
            //Get the position index of the text range in the paragraph
            int index = paragraph.ChildObjects.IndexOf(range);

            //Create a page break
            Break pageBreak = new Break(document, BreakType.PageBreak);
            //Insert the page break after the searched text
            paragraph.ChildObjects.Insert(index + 1, pageBreak);

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

C#/VB.NET: Insert Page Break into Word Documents

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.

Published in Page Setup
Page 2 of 2