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:
To insert a section in a document:
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"); |
To modify a section in a Word document:
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"); |
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. |
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"); |
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); |
To set the section properties using the Section object:
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"); |
For more information about how to implement sections using DsWord, see DsWord sample browser.