How to Load, Modify, and Save DOCX in C# .NET
Quick Start Guide | |
---|---|
Tutorial Concept | Learn how to programmatically load, modify, and save Word .docx files in a server-side application using a .NET Word API. With this API, developers can read existing DOCX files, make modifications, and save the updated files seamlessly using C#. |
What You Will Need |
Visual Studio (This example uses VS2022) .NET 8+ NuGet Packages: DS.Documents.Word |
Controls Referenced |
Enterprise-level software developers often need to programmatically manage Word documents in their applications, whether for document automation, content updates, or integrating Word processing into enterprise workflows. A powerful .NET Word API allows developers to efficiently load, modify, and save DOCX files without relying on Microsoft Word, ensuring seamless server-side document handling at scale.
With these APIs, developers can open existing Word documents, make precise modifications—such as updating text, inserting images, or adjusting formatting—and then save the updated files programmatically. This streamlined approach enhances efficiency, improves document consistency, and enables automation across business applications.
In this guide, we’ll demonstrate how to use Document Solutions for Word, a .NET Word API, to programmatically load, modify, and save DOCX files. You'll see how it simplifies document processing in enterprise applications while maintaining full control over content and formatting.
Download the Latest Release of Document Solutions for Word Today!
Steps to Generate a Word DOCX File Using C# .NET
- Create a .NET Console Application – Includes installing a .NET Word API
- Load an Existing DOCX File into a .NET Application - Invoking the Load Method
- Update Images Contained in a DOCX Shape
- Text Replacement with Regular Expressions
- Add a Hyperlink to the Loaded DOCX File
- Save the Updated Word .DOCX Document – Invoking the Save Method
Download a finished C# .NET 8 Console sample application here.
Create a .NET Console Application
Open Visual Studio 2022 and create a new Console App—for this example, we create a C# app and name it DsWord_Load_DOCX.
For this example, we will be using .NET 8.
Right-click the project in the Solution Explorer and choose Manage NuGet Packages. In the Package source at the top right, select NuGet.org. Click the Browse tab on the top left and search for "DS.Documents." On the left panel, select DS.Documents.Word.
Add the needed namespace to the Program.cs file:
Initialize the server-side Word DOCX file instance by invoking the GcWordDocument class in the main function of Program.cs. This creates a new .NET Word document object.
Load an Existing DOCX File into the .NET Application
Invoke the .NET Word API’s Load method to open an existing MS DOCX file into an instance of the GcWordDocument class.
Check out the Load an MS Word DOCX File online demo.
Update Images Contained in a DOCX Shape
This .NET Word API automates graphic updates in Word documents. It supports various shapes and fill types—like ImageFill—allowing users to simply supply new images and have them automatically integrated into the DOCX file programmatically. In our flyer, the image is accessed via the ImageFill property of the FillFormat class and replaced using the EmbeddedImageData’s SetImage method.
Notice that the Flyer.docx file’s image has now been updated:
DOCX Text Replacement with Regular Expressions using C#
The DsWord API simplifies updating dynamic content. In this example, a regular expression replaces the flyer’s ever-changing house price by matching a dollar sign followed by one or more digits. Enable regex matching by setting the RegularExpressions property to true in either the FindOptions or FindReplaceOptions class.
Below is the resulting document generated after executing the above-defined code to find and replace text.
Discover additional find and replace text in DOCX file techniques in our online explorer demos.
Add a Hyperlink to the Loaded DOCX File
The Document Solutions for Word’s API allows developers to easily convert plain text into a clickable hyperlink in a Word DOCX document. Using the Hyperlink class, you can automate this process, making it as simple as updating the document with the provided URL. For additional information, refer to the Links documentation.
With the code above implemented, notice the updated DOCX files now have a hyperlink.
Save the Updated Word .DOCX Document using C#
Save the server-side MS Word document using the APIs Save method of the DsWordDocument class.
Notice the updated and saved .NET DOCX file now contains the new image, updated price, and a hyperlink to the company's website.
Learn More About this C# .NET Word API
This article only scratches the surface of the full capabilities of Document Solutions for Word, our .NET Word API library. We recommend checking out another blog in this series, How to Programmatically Create Word DOCX Files in C# .NET, or visit our online demo explorer and documentation to learn more.
Ready to try it out? Download Document Solutions for Word Today!