Page Settings | Document Solutions for Word
Features / Document / Page Settings
Page Settings

DsWord stores all the page setup attributes, such as page borders, size, margins, etc., as properties in the PageSetup class. These properties control the structure and layout of pages in a word document. DsWord also allows you to insert a page break which is especially required in case of a long document using Type property of the Break class. The Type property takes Page as a value from BreakType enumeration for specifying a page break. Moreover, DsWord lets you specify how a document is printed using Type property of the MultiPagePrinting class. The Type property takes the value from MultiPagePrintingType enumeration, so that the printed document can be bound as a booklet.

Set Page Properties

To set the page properties:

  1. Access the page setup properties using PageSetup property of the Section class.
  2. Set the page size properties. For example, set the orientation of the page using the Orientation property and paper size using the PaperSize property.
  3. Set the page borders of the section using the Borders property and apply border to the pages using the Border class properties.
    C#
    Copy Code
    GcWordDocument doc = new GcWordDocument();
    doc.Load("SampleDoc.docx"); 
    Section first = doc.Body.Sections.First;
    
    first.PageSetup.Size.Orientation = PageOrientation.Landscape;
    first.PageSetup.Size.PaperSize = PaperSize.PaperLetter;
    
    first.PageSetup.Borders.AppliesTo = PageBorderAppliesTo.AllPages;
    first.PageSetup.Borders.Left.LineStyle = LineStyle.BabyPacifier;
    first.PageSetup.Borders.Right.LineStyle = LineStyle.BabyPacifier;
    first.PageSetup.Borders.AlwaysInFront = true;
    
    doc.Save("SetPageProperties.docx");
Back to Top

Set Page Number

To set the page numbering:

  1. Access the page setup properties using PageSetup property of the Section class.
  2. Set the page numbering properties. For example, set the starting page number using the StartingNumber property and the page number format using NumberStyle property of the PageNumbering class.
    This displays the page numbering when you click on the scroll handle, which is the default behavior. However, if you want to display the page number at the bottom of the page, you can insert a footer in the page and use a simple field in it.
  3. Append a footer using the Footers property and add a paragraph to it using the Add method.
  4. Add a simple field in the paragraph using Add method of the SimpleFieldCollection class.
    C#
    Copy Code
    doc.Load("SampleDoc.docx"); 
    Section first = doc.Body.Sections.First;
    
    //Set page numbering
    first.PageSetup.PageNumbering.StartingNumber = 3;
    first.PageSetup.PageNumbering.NumberStyle = NumberStyle.NumberInDash;
    first.Footers[HeaderFooterType.Primary].Body.Paragraphs.Add("");
    first.Footers[HeaderFooterType.Primary].Body.Paragraphs.First.GetRange().SimpleFields.Add("PAGE");
    
    //Save the document
    doc.Save("PageNumbering.docx");
Back to Top

Insert Page Break

To insert page break in a Word document:

  1. Access a paragraph in a section. For example, access first paragraph of the first section.
  2. Add a page break on the desired location in the paragraph using AddBreak method of the TextCollection class. For example, add the page break after first run of the first paragraph.
  3. Set the break type to Page using the BreakType enum.
    C#
    Copy Code
    doc.Load("SampleDoc.docx");
    
    //Access the first paragraph of the first section
    Section first = doc.Body.Sections.First;
    Paragraph p1 = first.GetRange().Paragraphs.First;
    
    //Insert page break
    Break br1;
    br1 = p1.GetRange().Runs.First.GetRange().Texts.AddBreak(BreakType.Page);
        
    //Save the document
    doc.Save("SampleDoc_PageBreak.docx");
Back to Top

Remove Page Break

To remove page break:

  1. Get a list of all the breaks of type Page from the document.
  2. Remove a page break using the Delete method. For example, remove the first page break.
    C#
    Copy Code
    //Load the document
    doc.Load("SampleDoc_PageBreak.docx");
    
    //Get a list of all the breaks of type "Page" that exist in the document
    var breaks = new List<Break>();
    foreach (var text in doc.Body.Texts)
    {
        Break x = text as Break;
        if (x != null && x.Type == BreakType.Page)
            breaks.Add(x);
    }
    
    //Remove the first page break
    breaks[0].Delete();
    
    //Save the document
    doc.Save("SampleDoc_NoPageBreak.docx");
Back to Top

Multipage Printing

To print a multiple page document:

  1. Access the options to print the multiple page document using MultiPagePrinting property of the PageSetup class.
  2. Set the type for document printing using Type property of the MultiPagePrinting class.
    C#
    Copy Code
    doc.Load("SampleDoc.docx");
    //Two out of the three sheets in the document will be printed on one page 
    doc.Body.Sections.First.PageSetup.MultiPagePrinting.Type = 
        MultiPagePrintingType.TwoPagesPerSheet;
    
    doc.Save("MultiPagePrinting.docx");
Back to Top

For more information on how to work with pages and page breaks using DsWord, see DsWord sample browser.