We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Setting a background color for the table of contents

Thread ID:





145416 Jun 20,2019 12:55 PM UTC Jun 25,2019 04:34 PM UTC ASP.NET Web Forms 5
Tags: DocIO
David Homer
Asked On June 20, 2019 12:55 PM UTC


We are using DocIO to create a Word document (and then converting it to a PDF).

We have created a table of contents and want to set the background color, and found we can't do this directly but have to create a rectangular auto shape and set this with TextWrappingStyle to the back of the page.

This works in both Word and PDF, however...

If the table of contents is longer than one page the background is only on the first page, how do we

1. Stretch the image over multiple pages?
2. Enumerate each physical page in the table of contents section and put a shape on it?

        /// <summary>
        /// Adds the table of contents to the document.
        /// </summary>
        /// <param name="document">The document to which the table of contents should be added.</param>
        private void AddTableOfContents(WordDocument document)
            tocSection = (WSection)document.AddSection();
            tocSection.PageSetup.Margins.All = 20;
            tocSection.PageSetup.PageSize = new SizeF(612, 792);
            IWParagraph headingParagraph = tocSection.AddParagraph();
            IWTextRange heading = headingParagraph.AppendText("Table of Contents");
            IWParagraph paragraph = tocSection.AddParagraph();
            TableOfContent toc = paragraph.AppendTOC(1, 3);

        /// <summary>
        /// Add a background to the table of contents.
        /// </summary>
        private void AddTableOfContentsBackground()
            WParagraph paragraph = tocSection.Paragraphs[0];
            Shape shape = paragraph.AppendShape(AutoShapeType.Rectangle, tocSection.PageSetup.PageSize.Width, tocSection.PageSetup.PageSize.Height);
            shape.FillFormat.Color = Color.LightBlue;
            shape.LineFormat.Line = false;
            shape.HorizontalPosition = 0;
            shape.VerticalPosition = 0;
            shape.HorizontalAlignment = ShapeHorizontalAlignment.Left;
            shape.HorizontalOrigin = HorizontalOrigin.Page;
            shape.VerticalAlignment = ShapeVerticalAlignment.Top;
            shape.VerticalOrigin = VerticalOrigin.Page;
            shape.IsBelowText = false;
            shape.WrapFormat.TextWrappingStyle = TextWrappingStyle.Behind;



Mohanaselvam  Jothi [Syncfusion]
Replied On June 21, 2019 07:19 AM UTC

Hi David,

Thank you for contacing Syncfusion support.

The Word document is a flow document in which contents will not be preserved page by page; instead the contents will be preserved sequentially section by section. Each section may extend to various pages based on its contents like table, text, images etc.

Word viewer/editor renders the contents of the Word document page by page dynamically when opened for viewing or editing and this page wise rendered information will not be preserved in the document level as per the Word file format specification.

Whereas Essential DocIO is a non-UI component that provides a full-fledged document object model to manipulate the Word document contents. Hence it is not feasible to get contents page by page from Word document using DocIO.

To achieve your end requirement, we suggest you to set table of contents inside table and set background color for the table. Thus, if the table of contents exceeds a page, table also continues to next page which preserves background color for the table of contents. We have prepared the sample application for the same and it can be download from the below link:

In this sample, we have done the following things:
1. Creates a new Word document.
2. Add table and TOC inside table cell.
3. Applies background color for the table.
4. Add contents in Word document to generate TOC.
5. Update table of contents.
6. Convert the Word document into PDF.
7. Saves the PDF document

Please let us know if you have any other questions.

Mohanaselvam J

David Homer
Replied On June 21, 2019 09:21 AM UTC

Hello, thank you for the reply.

Unfortunately there are white borders around this and the background stops part way through the last page.

Please see the screenshot comparing Syncfusion DOCIO with our existing implementation of TallPDF.



David Homer
Replied On June 21, 2019 09:22 AM UTC

Mohanaselvam  Jothi [Syncfusion]
Replied On June 24, 2019 06:40 AM UTC

Hi David,

Thank you for your update.

To achieve your requirement, we suggest you to set table of contents in separate section and add shape (rectangle shape with wrapping of behind text) in header of the section. So, if the table of contents exceeds the current page, the shape also preserved for the next page in the Word document. Please find the sample from the below link and let us know if it helps:

In this sample, we have done the following things:
  1. Create a new Word document.
  2. Create a new section.
  3. Add rectangle shape in header of current section.
  4. Add the table of contents
  5. Create a new section for other contents in Word document.
  6. Set empty header content for this section.
  7. Converts the Word document into PDF.
  8. Saves the PDF.

If the above solution doesn’t meet your requirement, could you please share us the input Word document and the expected Word document from your side. Thereby, we will check on your complete requirement and will provide you the solution at the earliest.

Please let us know if you have any other questions.

Mohanaselvam J 

David Homer
Replied On June 25, 2019 04:34 PM UTC

Ah interesting work around - thanks for your help :)




This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon