Insert Table of Contents in second page using aspose.words

I want to display the table of contents in second page rather than first page using Aspose.words dll. Can any one help me to solve the issue.

@rameshbeerla,

Sure, you can move cursor to a valid node inside second page and insert TOC.

To insert TOC at the start of second page, please use the following code:

Document doc = new Document("D:\\temp\\sample.doc");

Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = splitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection smallRuns = doc.getChildNodes(NodeType.RUN, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
for (int i=0; i<smallRuns.getCount(); i++)
{
    Run smallRun = (Run)smallRuns.get(i);
    if (collector.getStartPageIndex(smallRun) == 2) {
        builder.moveTo(smallRun);
        builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
        builder.writeln();

        break;
    }
    pageIndex++;
}

doc.updatePageLayout();
doc.updateFields();

doc.save("D:\\temp\\awjava-18.2.docx");

Hi Hafeez,

Thanks for the reply. I have tried inserting your code but i am receiving this error “‘Aspose.Words.NodeCollection’ does not contain a definition for ‘get’ and no extension method ‘get’ accepting a first argument of type ‘Aspose.Words.NodeCollection’ could be found (are you missing a using directive or an assembly reference?)”. Please check and suggest me the solution for it.

Error.png (7.6 KB)

@rameshbeerla,

Please ZIP and upload your input Word document here for testing. We will investigate the issue on our end and provide you more information.

@awais.hafeez

Please find the attached zip file.GenerateDocument.zip (2.4 KB)

@rameshbeerla,

Please provide a simple Word document for which the previously shared code is failing?

@awais.hafeez

Please find the attached word document which i used to generate the word document.
Template.zip (27.8 KB)

@rameshbeerla,

The following code produces correct output (see 18.2.zip (25.6 KB))

Document doc = new Aspose.Words.Document(MyDir + @"Template\Template.docx");

Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
for (int i = 0; i < smallRuns.Count; i++)
{
    Run smallRun = (Run)smallRuns[i];
    if (collector.GetStartPageIndex(smallRun) == 2)
    {
        builder.MoveTo(smallRun);
        builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
        builder.Writeln();

        break;
    }
    pageIndex++;
}

doc.UpdatePageLayout();
doc.UpdateFields();

doc.Save(MyDir + @"Template\18.2.docx");

private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring((0), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

@awais.hafeez

Thanks. Solution is working

1 Like

A post was split to a new topic: Insert Table of Contents TOC Field in Particular Page of Word Document | C# .NET

A post was merged into an existing topic: Insert Table of Contents TOC Field in Particular Page of Word Document | C# .NET