This C# project demonstrates how to use the OpenXML SDK library to open a Word document (.docx) and replace specific text with new text.
- .NET SDK: Ensure that you have the .NET SDK installed. Download it from the official .NET website.
- Open XML SDK: This project uses the OpenXML SDK library to manipulate Word documents.
Install the OpenXML SDK package via NuGet:
dotnet add package DocumentFormat.OpenXmlThis project contains a Docx class with a constructor that takes the path of the source Word file as a parameter.
There is then a ReplaceText method that takes the text to be replaced and the new text as parameters.
Finally, there is a Save method that takes the path of the target Word file as a parameter.
For now, the idea is simply to copy this class into a project (and evolve it as needed). I have intentionally made this a console project to demonstrate its usage.
Clone this repository and open it in your preferred C# editor to test, or simply copy the Docx.cs file into your own project.
Place a .docx Word document in the project folder or specify a path to an existing Word file. In the example folder, I created a Word file containing 2 placeholders to be replaced. The first is #name, which is written in a fragmented manner, and the second is #year, which appears in multiple locations including a text box.
The main code for text replacement is in the ReplaceText method. Here is an example in Program.cs showing how to use this code:
using ReplaceTextInWordDocument;
// Load the Word document
using var docx = new Docx("../example/example.docx");
// Perform successive replacements in the Word document (replacement is deliberately case-insensitive)
docx.ReplaceText("#name", "John Doe");
docx.ReplaceText("#year", DateTime.Now.Year.ToString());
// Save the new version of the document
docx.Save("../example/exampleOut.docx");In your terminal, navigate to the project folder and run the following command:
dotnet runThe specified text in example.docx will have been replaced. You can open the Word document to verify the replacements.
- Case Sensitivity: This code is case-insensitive. If you want to make it case-sensitive, adjust the logic to search for text with case sensitivity.