We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Table of Contents TOC Field in Particular Page of Word Document | C# .NET

Hi Hafeez ,

I was looking for a solution to insert toc from page and it was not working when i have table in page2…iam getting toc inside table in page2 when try ur code… can you help me with this issue if possible.

@k.sukumar,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word document you want to insert TOC field in
  • Your expected DOCX file showing the desired output. You can create this output file manually by using MS Word.

As soon as you get these pieces of information ready, we will then start further investigation into your particular scenario and provide you code to achieve the same expected output by using Aspose.Words.

toc new file.zip (693 Bytes)

This is the file which iam working on it the code which is there in this file used to start toc from page 1 but i want to start toc from page2 … iam getting issue when there is table in page2 starting …so please check the document

@k.sukumar,

Unfortunately, it is difficult to say what the problem is without these documents. We need your specific files to be able to reproduce the exact problem on our end. Please note that it is safe to attach files in the forum. If you attach your files here, then only you and Aspose staff members can download them. You can also remove any sensitive information by replacing it with dummy data instead (make sure that the problem is still reproducible with the simplified / modified files). We will then be able to provide you code to achieve the same expected output by using Aspose.Words.

Hi Hafeez this is document related to page2 issue iam inable to insert toc in page2 if iam trying to insert toc in page2 it is inserted inside of table but i want to insert outside of tableToc_starts_from.zip (424.0 KB)

@k.sukumar,

The following code should insert TOC field after the Table on second page:

Document doc = new Document("C:\\Temp\\Toc_starts_from\\Toc_starts_from.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.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 = 2;
foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        if (run.GetAncestor(NodeType.Table) == null)
        {
            builder.MoveTo(run);
            builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
            builder.Writeln();

            break;
        }
    }
}

doc.UpdateFields();

doc.Save("C:\\Temp\\Toc_starts_from\\21.9.doc");

public 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;
}

Hi Hafeez,

This solution is working fine but if we have a paragraph or figure after table it is not working . iam getting output at last page of document can you help me to resolve this issue… i have attached the documents regardingsrinu code (2).zip (412.2 KB)
the issue

@k.sukumar,

While using the latest 21.9 version of Aspose.Words, I have produced an output Word DOC document by using the code from my previous post and attached it here for your reference (see 21.9.zip (402.6 KB)). Do you see the same problem in this output? Can you please provide screenshot (encircle the problematic area in this DOC file)?

Hi Hafeez ,

The above code is working in console in our project we are getting wrong output please check the files which i have attached . and is it possible to write to code using paragraphs instead of runsissue docs.zip (809.3 KB)

we are using latest 21.9 version in our project

@k.sukumar,

Please open “Toc_starts_from (2).doc” that you attached in your previous post with MS Word. Go to the desired page and manually insert Table of Contents in it by using MS Word. Save As the Word file to DOC format and attach this expected DOC file (showing the desired behavior) here for our reference. We will then provide you code to get the same desired output by using Aspose.Words.

Please try the following code:

Document doc = new Document("C:\\Temp\\issue docs\\Toc_starts_from (2).doc");
DocumentBuilder builder = new DocumentBuilder(doc);

LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 2;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (collector.GetStartPageIndex(para) == pageIndex)
    {
        if (para.GetAncestor(NodeType.Table) == null)
        {
            builder.MoveTo(para);
            builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
            builder.Writeln();

            break;
        }
    }
}

doc.UpdateFields();

doc.Save("C:\\Temp\\issue docs\\21.10.doc");

Hi hafeez,
i want to insert table of contents before table…example a document has table in page2 starting and i need to send that table to next page and insert toc in page2. can you help me

QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g.zip (95.5 KB)
this file for example

@k.sukumar,

In this case, please use the following code to move the table to the start of third page.

Document doc = new Document(@"C:\Temp\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g\\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g.doc");
LayoutCollector collector = new LayoutCollector(doc);

// Get the table that you want to move to next page
Table table = doc.FirstSection.Body.Tables[0];

// Get the first Paragraph of page 3
int pageIndex = 3;
Paragraph paragraph = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (collector.GetStartPageIndex(para) == pageIndex)
    {
        paragraph = para;
        break;
    }
}

// insert table at the start of 3rd page
paragraph.ParentNode.InsertBefore(table, paragraph);

doc.Save(@"C:\Temp\\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g\\21.10.docx");

Thanks for help hafeez
:slightly_smiling_face:

Hi Haffez ,

when we create toc at that we create toc title also . i have issue with this title which need to apply font family styles to that particular title but when i applied font family to that tilte it is applied to everywhere in the document . will help me to do this ihave attached files below in that i have marked title please see it once … the tiltle which i circled to that particular one i have apply the font family (ex of font family ---- coutoc.zip (2.3 MB)
rier, calibri, timesnewroman)

@k.sukumar,

Please try the following C# code to adjust font formatting of TOC Title/Heading paragraph:

Document doc = new Document("C:\\Temp\\toc (1)\\QC0459_Output_2.3 Introduction KY.doc");

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
    if (para.ParagraphFormat.StyleName.Trim().Equals("TOC Heading Centered") && para.Range.Text.Trim().Equals("TABLE OF CONTENTS"))
        foreach (Run run in para.Runs)
        {
            run.Font.Name = "Verdana";
            run.Font.Bold = true;
        }

doc.Save(@"C:\Temp\\toc (1)\\21.10.doc");

Thanks for the help hafeez but iam getting same output as before which i have send you the document… is there any chance without using toc heading centered can we apply this verdana to this toc title name only …toc6.zip (100.6 KB)

please use source file not the output file … by using source we have to generate toc and for that generated toc title only we have to apply font family style… if we use source file in that we dont have any style before for the 3.2.P.3.5 Process Validation and or Evaluation - Media Fill_DMPA-SC-ACTD_Test.zip (39.4 KB)
document … so if we use source file we can get right output

@k.sukumar,

Considering the “3.2.P.3.5 Process Validation and or Evaluation - Media Fill_DMPA-SC-ACTD_Test.doc” as the source file, please also generate output Word DOCX containing the TOC and title and provide this output Word file showing the undesired behavior here for further testing. Please also create a standalone simple console application (source code that you are currently using without any compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose DLL files in it to reduce the file size.