How to find and replace 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 text with a Table of Contents (TOC) in a Word document using C#.
Steps to find and replace 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 a 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 a trial 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 the Program.cs file to find text and replace it with TOC in a 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 following helper methods to insert the TOC into the Word document.
// Insert the TOC at the index of the 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 the new paragraph into 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 text with a Table of Contents (TOC) in a 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 Word documents, find and replace text in the Word document, protect Word documents, and most importantly, the PDF and Image conversions with code examples.
See Also
- How to replace particular text with a hyperlink in a Word document
- How to replace text in a Word document with HTML
- How to find and replace text inside a table in a Word document
- How to find and replace text in headers and footers of a Word document
- How to find and replace a placeholder with a page break in a Word document
- How to find and replace line breaks in a Word document as a paragraph mark
- How to find and replace text in a Word document with text from a database
Conclusion
I hope you enjoyed learning how to find and replace text with a Table of Contents (TOC) in a Word document.
You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example 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!