Free Support Forum - aspose.com

Changing the field code in a TOC field

Is there a way to modify the field code of an existing TOC field?

I want to change the field code from { TOC } to { TOC \o “1-2” }

Thanks,
Tim.

@tim111,

Please try running the following code:

Document doc = new Document("C:\\Temp\\toc.docx");

FieldToc fieldToc = null;
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldTOC)
    {
        fieldToc = (FieldToc)field;
        break;
    }
}

if (fieldToc != null)
{
    // A field's code resides between Start & Separator nodes
    // Let's remove all nodes between Start & Separator nodes
    Node currentNode = fieldToc.Start.NextPreOrder(doc);
    while (currentNode != fieldToc.Separator)
    {
        Node nextNode = currentNode.NextPreOrder(currentNode.Document);
        currentNode.Remove();
        currentNode = nextNode;
    }

    // Add a new field code
    Run field_Code = new Run(doc, " TOC \\o \"1-2\" ");
    fieldToc.Start.ParentNode.InsertAfter(field_Code, fieldToc.Start);
    fieldToc.Update();
}

doc.Save("C:\\temp\\20.11.docx");

Alternatively, you can use different properties of FieldToc Class to reflect changes in Table of Content’s field code. For example:

Document doc = new Document("C:\\Temp\\toc.docx");

FieldToc fieldToc = null;
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldTOC)
    {
        fieldToc = (FieldToc)field;
        break;
    }
}

if (fieldToc != null)
{
    fieldToc.HeadingLevelRange = "1-2";
    //fieldToc.EntrySeparator = "-";
    //fieldToc.InsertHyperlinks = true;
    //fieldToc.HideInWebLayout = false;
    //fieldToc.PreserveLineBreaks = true;
    //fieldToc.PreserveTabs = true;
    //fieldToc.UseParagraphOutlineLevel = false;
}

doc.UpdateFields();
doc.Save("C:\\temp\\20.11.docx");

Hi, Awais.

Thank you for taking the time to send me this. I did eventually figure this out on my own. Here’s what I did:

    System.out.println("Removing current TOC field...");
    int fieldCount = doc.getRange().getFields().getCount();
    for ( int fc = 0; fc < fieldCount; fc++ ) {
        if (doc.getRange().getFields().get(fc).getFieldCode().contains("TOC")) {
            doc.getRange().getFields().get(fc).remove();
            System.out.println("Removed TOC field code.");
            break;
        }
    }
    
    // Get paragraph you want to append this TOC field to
    System.out.println("Finding the Contents Heading paragraph...");
    NodeCollection<Paragraph> paras = (NodeCollection<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph para : paras
            ) {
        if (para.getParagraphFormat().getStyleName().contentEquals("Contents Heading")) {
            Paragraph tocpara = (Paragraph) para.getNextSibling();
            tocpara.appendField(" TOC \\h \\o \"1-2\"");
            System.out.println("TOC field inserted.");
            break;
            }
        }
            
    
    //Update all fields
    doc.updateFields();

@tim111,

Yes, you can use Aspose.Words for Java to first remove the Table of Contents (TOC) field and then insert a new TOC field at the same place (Paragraph) in Word document. It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.