
- #HOW TO UPDATE WORD TABLE OF CONTENTS ONLY PAGE NUMBERS HOW TO#
- #HOW TO UPDATE WORD TABLE OF CONTENTS ONLY PAGE NUMBERS CODE#
A TOC with no switches will include content from the built-in heading styles (as if the \O switch is set). The available TOC switches that are supported by Aspose.Words are listed below and their uses are described in detail. Switches can be combined together to allow complex table of contents to be produced.īy default these switches above are included when inserting a default TOC in the document. Certain switches are used to control which entries are included and at what level while others are used to control the appearance of the TOC.
#HOW TO UPDATE WORD TABLE OF CONTENTS ONLY PAGE NUMBERS CODE#
Using Switches to Control the Behavior of the Table of Contents.Īs with any other field, the TOC field can accept switches defined within the field code that control the how the table of contents is built. The correct page numbers calculated from this call are then inserted into the TOC. This needs to be done to gather the page numbers of the entries. The only thing missing is the page numbers which for now are displayed with “?”. The second call to Document.UpdatePageLayout will build the layout of the document in memory. The first call to Document.UpdateFields will build the TOC, all text entries are populated and the TOC appears almost complete.
#HOW TO UPDATE WORD TABLE OF CONTENTS ONLY PAGE NUMBERS HOW TO#
These methods will automatically update all TOCs found in the document. Below code sample shows how to completely rebuild TOC fields in the document by invoking field update. Any number of different TOCs can be updated. If reversed the table of contents will be populated but no page numbers will be displayed.

Please note that these two update methods are required to be called in that order. This can be done to populate a newly inserted TOC or to update an existing TOC after changes to the document have been made. The following two methods must be used in order to update the TOC fields in the document: Updating the Table of ContentsĪspose.Words allows you to completely update a TOC with only a few lines of code. Further information about this is discussed in the next section. This is because the TOC field has been inserted but is not yet populated until it’s updated in the document. Without these calls when the output document is opened you would find that there would be a TOC field but with no visible content. The next lines then populate the TOC by updating the fields and page layout of the document. The DocumentBuilder class is then used to insert some sample content formatting with the appropriate heading styles which are used to mark the content to be included in the TOC. The code demonstrates the new table of contents being inserted into a blank document. Below code sample shows how to insert a Table of Contents field into a document.īelow code sample shows how to insert a Table of contents (TOC) into a document using heading styles as entries. You can either use that guide obtain the correct switches or if you already have a document containing the similar TOC that you want you can show field codes ( ALT+F9) and copy the switches directly from the field. Descriptions of these switches as well as a list of supported switches can be found later in the article. The default switches that are used in a TOC inserted in Microsoft Word are “\o “1-3 \h \z \u”. The field switches that you pass to the method control the way the table is built and displayed in your document. You can insert a TOC (table of contents) field into the document at the current position by calling the DocumentBuilder.InsertTableOfContents method.Ī table of contents in a Word document can be built in a number of ways and formatted using a variety of options.

Insert a Table of Contents Programmatically How to remove an entire TOC field along with all entries form the document.


