How to find and replace a text with Table of Contents (TOC) in Word document?
Syncfusion DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can find and replace a text with Table of Contents (TOC) in Word document using C#.
Steps to find and replace a text with Table of Contents using C#
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your application from NuGet.org.
Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
- Include the following Namespaces in the Program.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
- Add the following code snippet in Program.cs file to find a text and replace with TOC in Word document.
static void Main(string[] args)
{
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Opens an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
TextSelection[] selections = document.FindAll("[Insert TOC]", true, true);
WTextRange textrange = selections[0].GetAsOneRange();
WParagraph paragraph = textrange.OwnerParagraph;
int index = paragraph.ChildEntities.IndexOf(textrange);
//Remove the existing text.
paragraph.ChildEntities.Remove(textrange);
//Insert the TOC.
InsertTOC(document, paragraph, index);
//Update the TOC.
document.UpdateTableOfContents();
//Creates file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}
- Add the below helper methods to Insert the TOC in the Word document.
// Insert the TOC in the index of given paragraph.
private static void InsertTOC(WordDocument document, WParagraph ownerPara, int index)
{
//Create a new paragraph.
WParagraph newPara = new WParagraph(document);
//Append TOC to the new paragraph.
newPara.AppendTOC(1, 3);
//Insert the child entities of new paragraph to the owner paragraph at the given index.
for (int i = 0; i < newPara.ChildEntities.Count;)
{
ownerPara.ChildEntities.Insert(index, newPara.ChildEntities[i]);
index++;
}
}
A complete working sample to find and replace a text with Table of Contents(TOC) in Word document using C# can be downloaded from GitHub.
By executing the program, you will get the Word document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge , split and compare documents, find and replace text in the Word document, and most importantly PDF conversion with code examples.
See Also
- How to replace the particular text with hyperlink in Word document
- How to replace text in a Word document with HTML
- How to find and replace text inside table in Word document
- How to find and replace text in headers and footers of Word document
- How to find and replace placeholder with page break in Word document
- How to find and replace line break in Word document as paragraph mark
- How to find and replace in Word document with text from database
Conclusion
I hope you enjoyed learning how to find and replace a text with Table of Contents (TOC) in Word document.
You can refer to ASP.NET Core control feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our
ASP.NET Core Examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or
feedback portal. We are always happy to assist you!