How to find and replace the placeholder with an incremental value in a Word document?
Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can find and replace the placeholder with an incremental counter in a Word document in C#.
Steps to find and replace the placeholder with an incremental counter in a Word document using C#
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file:
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
- Use the following code example to find and replace placeholder with an incremental counter in a Word document in C#.
C#
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Open an existing Word document. using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) { //Find all occurrence of the Word "{counter}" in the document. TextSelection[] textSelection = document.FindAll("{counter}", false, false); //Iterate through each occurrence and change the text as an incremental value. int counter = 1; foreach (TextSelection selection in textSelection) { IWTextRange textRange = selection.GetAsOneRange(); textRange.Text = counter.ToString(); counter++; } //Create file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save the Word document to file stream. document.Save(outputFileStream, FormatType.Docx); } } }
A complete working sample to find and replace the placeholder with an incremental counter in a Word document in C# can be downloaded from GitHub.
By executing the program, you will get the output document as follows. Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
See Also:
How to find and replace the placeholder with a page break in a Word document
How to find and replace text with content control in a Word document?
How to find and replace line breaks in a Word document as a paragraph mark?
Conclusion
I hope you enjoyed learning about how to find and replace the placeholder with an incremental value in a Word document
You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET Core 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 ASP.NET Core DocIO and other ASP.NET Core components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!