Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Table Of Contents using OpenXML SDK 2.0?

Using the SDK I'm building Word documents that contain reports. These documents need to have TOC. Does anybody have a complete solution that I can follow in order to understand how to do this?

(I've read everything on http://openxmldeveloper.org/)

like image 615
Yeseanul Avatar asked Mar 18 '12 21:03

Yeseanul


People also ask

What is OpenXML SDK?

The Open XML SDK 2.5 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package. The Open XML SDK 2.5 encapsulates many common tasks that developers perform on Open XML packages, so that you can perform complex operations with just a few lines of code.

How do I add DocumentFormat OpenXML reference?

Go to your Solution Explorer > right click on references and then click Manage NuGet Packages . Then search in tab "Online" for DocumentFormat. OpenXml and install it.

What is DocumentFormat OpenXML?

The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as: - High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.


1 Answers

Have a look at Fourth and Final Screen-Cast in Series on Adding/Updating the TOC in OpenXML WordprocessingML Documents by Eric White.

Hope that helps!

UPDATE:


According FAQ from MSDN Forums I see that this feature is not supported:

8) How to generate TOC (table of contents) in Word document?

Open XML SDK 2.0 does not have this feature supported. But you can generate a small TOC through Word app, and reflect the TOC parts with Document Reflector component in Open XML SDK Productivity Tool to see how to generate a TOC programmatically. For more detailed information, please refer to:

  • MSDN forum thread: Generating Table of Contents and Applying a Custom Style,

  • MSDN forum thread: How can i Create dir for word document.


UPDATE 2


Based on our comments below I could propose to use this scenario:

  1. You manually create an empty DOCX file and insert TOC inside it.
  2. Then you save this file and open it in OpenXML SDK 2.0 Tool, which provides you with the C# code to generate such empty file with TOC placeholder inside.
  3. Then you programmatically flush all the data you need to this DOCX file and save it.
  4. In addition you will need provide the mechanism that will auto update TOC once the data are flushed (or once the document is opened). There are a few options to do that - see screen-casts 3-5 fromthe link to Eric White post I provided above. Especially, I think youshould pay your attention to 5th one - "Shows how to use an AutoOpen macro to update the TOC whenever any document that contains a TOC is opened".

All of that look a bit tricky, but I hope that helps.

like image 158
Dmitry Pavlov Avatar answered Sep 21 '22 18:09

Dmitry Pavlov