1
votes

I'm using OpenXML on a asp.net website to create a Word document from a template and adding additional sections to the template from other templates via an altChunk as well as adding HTML fragments (also as altChunk) and images. Everything is working fine except that my template has a table of contents (TOC) and to update it with both the correct page numbers and the correct headings (for the added sections), the end user has to select the TOC and update it (by hitting F9, or clicking update field from the context menu). Even then they still have to choose between updating just page numbers or updating the whole table (confusing for the end user).

I'd really like to avoid this step because I'd like the document to be ready to go and not look like it was autogenerated on the backend.

I tried adding a UpdateOnFieldsOpen to the document settings, but that only causes a confusing (to the end user) nag screen when they open the document.

I thought about maybe trying to manually update the TOC in OpenXML by estimating the length (in pages) of each section I add (they should probably be fairly constant +/- a page) to give at least a fairly accurate TOC when the user first opens the doc, but that doesn't seem very robust.

Word automation would potentially fix this, but it's not recommended for server-side.

Are there any better solutions for this? It seems silly that I can arbitrarily insert chunks of other documents and HTML without any complaint, but updating the TOC is a major security concern!

1

1 Answers

1
votes

As you've no doubt become aware, this issue with updating a TOC automatically is a common complaint.

In the Java world, the docx4j enterprise (ie commercial) edition can solve it [disclosure: my company's], by generating the entire TOC independent of Word.

By the way, the docx4j community edition can also convert XHTML altChunk to native/raw WordML, which is an important first step for getting the TOC right. You can use it in a .NET environment; see http://www.nuget.org/packages/docx4j-ImportXHTML.NET/3.0.1

A left-field solution might be to invoke LibreOffice (eg via JODConverter); it'll update a TOC automatically without prompting.