Problem with TOC in Word document

Hi,
I’ve been trying to find a solution to this problem on your forums but have not found anything conslusive.

When updating an existig document containing a TOC with the following field code

{ TOC \h \z \t “Heading 3;1;Heading 4;2” }

Aspose Words will print No table of contents entries found. in the saved document.

Right-clicking on the the field in Word and selecting Update field will yield a correct TOC. Running it through Aspose Words again will give the initial result.

var document = new Document("test.docx");
document.UpdateFields();
document.Save("test.docx", SaveFormat.Docx);

Please advise

So I have some additional information here. We use an international version of Word and after inspection of the docx markup we have concluded that this is indeed a bug in your code.

In swedish heading 1 is Rubrik1 and this is reflected in the XML source. that looks like this

<w:style w:type="paragraph" w:styleId="Rubrik1"><w:name w:val="heading 1"/><w:aliases w:val="Omslagsrubrik"/><w:basedOn w:val="Normal"/>

My original field code that works in Word but failes in Aspose looks like this
{ TOC \h \z \t “Rubrik2;1;Rubrik3;2” }

By changing this manually to the following i get the same failure in Word, but now it WORKS in Aspose.
{ TOC \h \z \t “heading 2;1;heading 3;2” }

Both Rubrik1 and heading 1 are present in the markup, but where as Word uses w:styleId you seem to incorrectly reference w:name w:val causing the TOC to fail rendering properly in both products.

Screen capture of the document after it has been saved by Aspose.Words
image.png (7.9 KB)

Screen capture of what the document looks like after you right-click and select update field on each TOC
image.png (8.0 KB)

And the document with field codes visible
image.png (7.6 KB)

I have multiple accounts here it seems. This is the correct one to reply to.

And the document in question here
sdfggdsfds_f3800847-26df-4688-8303-e66ba0b7f51d.zip (12.6 KB)

@PatrikM

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you update the TOC field under English culture by using MS Word, you will get the same output (no table of contents).

In your case, you need to update the TOC field according to your desired culture. Moreover, the list separator in TOC field should be correct. Please note that the list separator is semicolon “;” in Swedish culture. It is comma “,” in English culture. Please use the following line of code to set the culture code for current thread.

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("…culture code…");

Hi,
yes, well this is the answer you’ve provided in numerous other threads and I’ve already tried this with exactly the same result as described above. This is the actual code I use

    private byte[] SetProperties(byte[] documentBytes, IDictionary<string, string> properties, SaveFormat saveFormat)
    {
        using (var documentStream = new MemoryStream(documentBytes))
        using (var outputStream = new MemoryStream())
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("sv-SE");
            var document = new Document(documentStream);
            
            // We do various proprietary modifactions of the document here

            document.UpdateFields();
            document.Save(outputStream, saveFormat);
            return outputStream.ToArray();
        }
    }

@PatrikM

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-17413. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@tahir.manzoor
I’m happy that you acknowledge that this is a bug :slight_smile: I’m looking forward to see the issue resolved.

If you need any additional information please contact me here or drop me an e-mail and I’ll try to assist as best I can.

The issues you have found earlier (filed as WORDSNET-17413) have been fixed in this Aspose.Words for .NET 18.10 update and this Aspose.Words for Java 18.10 update.