Update TOC

Hi
Can you please provide sample code that will update Table of Content.
Regards
Nelson Marian

Thank you for your query.
You should call the UpdateFields method. As I don’t know what SDK or programming language you use, it’s challenging to create a sample for you, but as an example, I suggest you check our tests in SDK. They have examples for all API methods. aspose-words-cloud-dotnet/Aspose.Words.Cloud.Sdk.Tests/Api/Field/FieldTests.cs at a430d56000181ddd33c3c6c39c2c754a7895a9f9 · aspose-words-cloud/aspose-words-cloud-dotnet · GitHub as an example of a method you need.

The programming language which I am using is C# . NET version 4.0 .
Will this line of code will work.
// Load the document
string dataDir = @"D:\Documents";

        Aspose.Words.Document doc = new Aspose.Words.Document(dataDir + "TOC.docx");

        // Update all fields in the document, including the TOC
        doc.UpdateFields();

        // Update the page layout to ensure page numbers are calculated
        doc.UpdatePageLayout();

        // Save the updated document
        doc.Save(dataDir+ "TOC1.docx");

But I am getting this msg Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.

Can you provide the sample code other then the above one.
Regards
Nelson Marian

First of all, you are using Aspose.Words, not an Aspose.Words Cloud. You get this message because you didn’t set your license.

// Set the license for our Aspose.Words product by passing the local file system filename of a valid license file.
string licenseFileName = Path.Combine(LicenseDir, "Aspose.Words.NET.lic");

License license = new License();
license.SetLicense(licenseFileName);

// Create a copy of our license file in the binaries folder of our application.
string licenseCopyFileName = Path.Combine(AssemblyDir, "Aspose.Words.NET.lic");
File.Copy(licenseFileName, licenseCopyFileName);

// If we pass a file's name without a path,
// the SetLicense will search several local file system locations for this file.
// One of those locations will be the "bin" folder, which contains a copy of our license file.
license.SetLicense("Aspose.Words.NET.lic");

Hello sir,
Thanks for your quick response. Let me explain what our task is then according to that can you please suggest solution to us.
We wanted to update TOC for existing document. Our current code is in C# 4.5 version, we are using @“https://api.aspose.cloud”; and I do have “My App Key” and “My App Sid”.
I have completed code for Comparison of documents using above configuration.
But I am not finding end to end code for TOC update for existing documents. We are not going to make new document, but we are going to update TOC for our existing.
Can you please provide solution for this.
Regards,
Nelson Marian

The UpdateFields method updates all fields inside the document. TOC also will be updated after you have executed it.
In the example I shared above, you can see how to execute update field. If you don’t want to use storage, you can use the UpdateFieldOnline method aspose-words-cloud-dotnet/Aspose.Words.Cloud.Sdk.Tests/Api/Field/FieldTests.cs at a430d56000181ddd33c3c6c39c2c754a7895a9f9 · aspose-words-cloud/aspose-words-cloud-dotnet · GitHub

Hi Sir,
Thanks for link I have gone through the link and the code it has. What I am not getting is

  1. How to call that function UpdateFieldOnline using the WordsApi and how shall I pass my document dynamically.
  2. The other point what is the similar function for "UpdatePageLayout , which I can call using WordsApi .

Can you please provide me code that will help me how to call the functions and how shall I pass the document path dynamically.
Regards
Nelson Marian

The links I shared contain precisely what you are asking about calling the function and passing the document to the method.

using var requestDocument = File.OpenRead(LocalTestDataFolder + localFile);

Here is how you specify your document for passing to the method

 var request = new UpdateFieldsOnlineRequest(
                document: requestDocument
            );

this is a request initialization where you pass the document

var actual = await this.WordsApi.UpdateFieldsOnline(request);

this is a line where you call UpdateFieldsOnline method and result that this method returns is the document with updated fields.

var actual = await this.WordsApi.UpdateFieldsOnline(request);

  1. I am getting this error:- “WordsApi” does not contain a definition for UpdateFieldsOnline and no accessible extension method “UpdateFieldsOnline” accepting a first argument of type “WordsApi” could be found (are you missing a using directive or an assembly referent?)

  2. The return actual is the document with updated fields then do I need to rewrite this actual result again to make a document from it. (I am not understanding how to reengineer this actual result)

  3. Why can’t it just update the TOC and my document gets updated, and I don’t have to do anything.

The ‘await’ operator can only be used an async method. Consider making this method with the ‘async’ modifier changing its return type to ‘Task’.

  1. I am using WCF service , this lines of code will be part of WCF service.

  2. Do I have to make a separate function with and call this async method (How shall we achieve that.)

  3. Do I need to call “UpdatePageLayout” is there similar function to it .
    Regards,
    Nelson Marian

Please use the latest version of SDK, where UpdateFieldsOnline is available.
The return actual is a Stream, you can save it to file how you like.
You can do that, but not in the Cloud version. You can use Aspose.Words, and you don’t need to use anything except update page layout.