Document Solutions for Word
Features / Sections
In This Topic
    Sections
    In This Topic

    Sections in a word document allows you to insert, modify, remove sections and get and set section properties. A document can be divided into sections by inserting a section which creates a unique page layout for pages in that particular section. For instance, to create a portion of document with landscape orientation while leaving the rest as default portrait mode, insert a section before and after that landscape portion.

    In DsWord, sections of the document are represented by the Section class which allows you to access section element in the body of a document. The Section objects are immediate children of the Document's body and can be accessed via the Sections property.

    You can add sections to a document using AddSectionBreak method of the Paragraph class. This method accepts the section break type as a parameter, which can be passed using the SectionStart enumeration. The default value of this parameter is set to "NewPage", so invoking this method without a parameter inserts a section break of type new page in a document. However, the type of section break inserted can be altered by passing in appropriate value from the SectionStart enum. This enumeration has the following values:

    Sections in a Word document

    Insert Section

    To insert a section in a document:

    1. Create a new Word document using an instance of the GcWordDocument class.
    2. Access the first section of the document.
    3. Change the default page margins using Margin property of the PageSetup class.
    4. Add paragraphs to the section using the Add method of ParagraphCollection class.
    5. Add a new section in the document using AddSectionBreak method of the Paragraph class.
    6. Set orientation of the new section using Orientation property and add paragraphs to it.
      C#
      Copy Code
      //Create a document
      GcWordDocument doc = new GcWordDocument();
      //Acess the first section available by default
      Section sec1 = doc.Body.Sections.First;
      
      // Change default margins
      sec1.PageSetup.Margin.Left = sec1.PageSetup.Margin.Right =
      sec1.PageSetup.Margin.Top = sec1.PageSetup.Margin.Bottom = 36;
      
      //Add paragraphs to the first section
      ParagraphCollection pars1 = sec1.GetRange().Paragraphs;
      pars1.Add("Section 1. This is the first paragraph of the document.");
      pars1.Add("Section 1. This is the second paragraph of the document.");
      pars1.Add("Section 1. This is the third paragraph of the document.");
      
      //Add another section by inserting a section break
      Section sec2 = pars1.Last.AddSectionBreak();
      
      // Change page orientation for the second section
      sec2.PageSetup.Size.Orientation = PageOrientation.Landscape;
      
      //Add paragraphs in the second section
      ParagraphCollection pars2 = sec2.GetRange().Paragraphs;
      
      pars2.Add("Section 2. This is the first paragraph of second section.");
      pars2.Add("Section 2. This is the second paragraph of second section.");
      pars2.Add("Section 2. This is the third paragraph of second section.");
      
      //Save the document
      doc.Save("AddSection.docx");
    Back to Top

    Modify Section

    To modify a section in a Word document:

    1. Access the paragraph collection in a section.
    2. Add a new paragraph to the section.
    3. Set the paper size using PageSetup property of the Section class.
      C#
      Copy Code
      doc.Load("AddSection.docx");
                  
      //Access the first section
      Section sec1 = doc.Body.Sections.First;
      
      //Access the paragraph collection in the first section
      ParagraphCollection pars1 = sec1.GetRange().Paragraphs;
      
      //Add a new paragraph to the first section
      pars1.Add("Section 1. Adding fourth paragraph to the first section.");
      
      //Set the paper size
      sec1.PageSetup.Size.PaperSize = PaperSize.PaperA4;
      
      //Save the document
      doc.Save("ModifySection.docx");
    Back to Top

    Remove Section

    To remove a section or section break from a document, you can use one of the methods given below:

    Method Description
    MergeWithPrevious() Removes the section break between two sections and the section content becomes the content of the previous section.
    MergeWithNext() Removes the section break between two sections and the section content becomes the content of the next section.
    Delete() Removes the section and its whole content.
    1. Access the section in the document which needs to be removed.
    2. Remove the section break by merging the section with the previous section using MergeWithPrevious method of the Section class.
      C#
      Copy Code
      doc.Load("InsertSectionBreak.docx");
                  
      //Remove the section break
      Section sec = doc.Body.Sections.Last;
      
      //removing section break by merging a section with the previous one
      sec.MergeWithPrevious();
      
      doc.Save("RemoveSectionBreak.docx");
    Back to Top

    Get Section Properties

    To get the properties of a section, you need to access a particular section. For example, access the last section of the document to get the direction of the text flow using the TextFlowDirection enumeration.

    C#
    Copy Code
    SetSectionProperties();
    doc.Load("SetSectionProperties.docx");
    
    //Access the last section of the document
    Section sec1 = doc.Body.Sections.Last;
    
    //Get the direction of text flow
    TextFlowDirection TypeA = (TextFlowDirection)Enum.Parse(typeof(TextFlowDirection),
                               sec1.PageSetup.TextFlowDirection.ToString() );
    
    //Write the fetched direction of text flow on the console
    Console.WriteLine("TextFlowDirection: " + TypeA);
    Back to Top

    Set Section Properties

    To set the section properties using the Section object:

    1. Access a section to set its properties. For example, access the last section.
    2. Set the section properties. For example, set the direction of text flow in the section and the paper size for last section of the document.
    C#
    Copy Code
    doc.Load("AddSection.docx");
    
    //Access the last section of the document
    Section sec = doc.Body.Sections.Last;
    
    //Set the direction of text flow
    sec.PageSetup.TextFlowDirection = TextFlowDirection.TopToBottomRightToLeft;
    //Set the size of the paper for the last section
    sec.PageSetup.Size.PaperSize = PaperSize.PaperA4;
    
    doc.Save("SetSectionProperties.docx");
    Back to Top

    For more information about how to implement sections using DsWord, see DsWord sample browser.