Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

A Section object represents a major section in a document, like a chapter in a book. Many documents will only contain one section. In more complex documents, content can be divided into multiple sections.

 

You can assign a different set of formatting settings to each section, including:

...

  • Code Block
    Section Element.CreateSectionAfter()

    Inserts an empty section at the end of the specified Element.

  • Code Block
    Section Element.CreateSectionBefore()

    Inserts an empty section at the beginning of the specified Element.

Before adding a section to your file, create a Document object.

Code Block
c#
c#

WordApplication wwapp = new WordApplication();
Document doc = wwapp.Create();

Call Document.CreateSectionAfter or Document.CreateSectionBefore to add a section to the document

Code Block
c#
c#

//--- Create a section at the end of the document.
Section firstSection = doc.CreateSectionAfter();

Next, set Section properties, for example:

Code Block
c#
c#

//--- Add a footer to the section.  A header or
//--- footer can be applied to all pages in a section,
//--- the first page in a section, odd pages only, or
//--- even pages only.  The following returns a footer
//--- for all pages in the section.
Element footer = firstSection.GetFooter(Section.HeaderFooterType.All);
footer.InsertTextAfter("Copyright 2007 SoftArtisans Inc.", true);

//--- Set the width of a margin in twips (1/1440 in).
firstSection.LeftMargin = 1440;
firstSection.RightMargin = 1440;

After creating a Section and setting Section properties, add content. For example, add a paragraph:

Code Block
c#
c#

Paragraph para = firstSection.InsertParagraphAfter
     (doc.Styles[NamedStyle.BuiltIn.Normal]);
para.InsertTextAfter("OfficeWriter is now fully integrated and compatible
     with SQL Server Reporting Services. Business users
     can now create and publish their Reporting Services
     reports without ever leaving Excel and Word. With
     OfficeWriter, every feature of Excel and Word is
     preserved in Reporting Services.",
     true);

You can access existing sections through the Document.Sections property.

Code Block
c#
c#

//--- Open an existing Word file and get the first
//--- section.
WordApplication wwapp = new WordApplication();
Document doc = wwapp.Open(@"C:\sample.doc");
Section firstSection = doc.Sections[0];