Skip to Main Content
Adding more elements to your Document – Part V

Adding more elements to your Document – Part V

Learn more about Xceed Words for .NET

 

This week, we will look at how to add Headers/Footers and a Table of Contents, to your documents.

Headers/Footers

In order to add headers to a document, we must first call its AddHeaders method to generate them, this will generate the required sections in the document. Similarly, adding footers to a document is done by first calling its AddFooters method. You can choose to include only headers, or only footers, or both, as they are controlled separately.

// Generate the Headers/Footers sections for this document
document.AddHeaders();
document.AddFooters();

 

Once this is done, we need to specify what they should display by adding one or more paragraphs in each appropriate section. By default, the header/footer specified for the Odd section will be used for all pages in the document.

// Insert a paragraph in the Headers/Footers (used for all pages)
document.Headers.Odd.InsertParagraph(“This is the header for all pages”);
document.Footers.Odd.InsertParagraph(“This is the footer for all pages”);

 

Different Odd and Even pages

To display different headers/footers on odd and even pages, the DifferentOddAndEvenPages property on the document must be set to true.

// Indicate that the odd and even pages will have separate Headers/Footers
document.DifferentOddAndEvenPages = true;

// Insert a paragraph in the Headers/Footers for odd pages
document.Headers.Odd.InsertParagraph(“This is the header for odd pages”);
document.Footers.Odd.InsertParagraph(“This is the footer for odd pages”);

// Insert a paragraph in the Headers/Footers for even pages
document.Headers.Even.InsertParagraph(“This is the header for even pages”);
document.Footers.Even.InsertParagraph(“This is the footer for even pages”);

 

Different First page

To display different headers/footers on the first page, the DifferentFirstPage property on the document must be set to true.

// Indicate that the first page will have separate Headers/Footers
document.DifferentFirstPage = true;

// Insert a paragraph in the Headers/Footers for first page
document.Headers.First.InsertParagraph(“This is the header for the first page”);
document.Footers.First.InsertParagraph(“This is the footer for the first page”);

 

Table of Contents

A table of contents can be inserted anywhere in the document, but it is usually added either at the start of the document or at the end.

To insert a table of contents, the InsertTableOfContents method is called on the document. It has 2 overloads, one to specify prior to which paragraph it should be inserted, and another to append it at the end of the document

 

Inserting a Table of Contents at a specific location in the document.

// Get a reference to the document's first paragraph
var firstParagraph = document.Paragraphs[ 0 ];

// Add the Table of Content prior to the referenced paragraph
var toc = document.InsertTableOfContents( firstParagraph, "Table of Contents", TableOfContentsSwitches.None );

// Add a page break prior to the referenced paragraph so it starts on a fresh page after the Table of Content
firstParagraph.InsertPageBreakBeforeSelf();

 

Inserting a Table of Contents at the end of the document.

// Add a page break to place the Table of Content on a new page
document.InsertSectionPageBreak();

// Add the Table of Contents
document.InsertTableOfContents( "Table of Contents", TableOfContentsSwitches.None );

 

Updating an existing Table of Contents

// Force an update of the Table of Content.
document.UpdateFields();

  

For more information, please refer to the documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple