Skip to main content Skip to footer

How to Load, Modify, and Save DOCX in C# .NET

  • 0 Comments
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

Document Solutions for Word - .NET Word API

Online Demo Explorer | Documentation | Other Word API Blogs

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

  1. Create a .NET Console Application – Includes installing a .NET Word API
  2. Load an Existing DOCX File into a .NET Application - Invoking the Load Method
  3. Update Images Contained in a DOCX Shape
  4. Text Replacement with Regular Expressions
  5. Add a Hyperlink to the Loaded DOCX File
  6. 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.

Create a C# Console Application

For this example, we will be using .NET 8.

Create a .NET 8 Console App to Create a DOCX File

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.

.NET Word API NuGet Package - Ds.Documents.Word

Add the needed namespace to the Program.cs file:

using GrapeCity.Documents.Word;

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.

// Create a new Word document:
GcWordDocument doc = new GcWordDocument();

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.

// Load an existing Word document
doc.Load("Flyer.docx");

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.

// Update ImageFill for a shape
var newImage = new FileStream("NewHouse.jpg", FileMode.Open);
doc.Body.Shapes[6].Fill.ImageFill.SetImage(newImage, "image/jpg");
newImage.Close();

Notice that the Flyer.docx file’s image has now been updated:

DOCX File Image in Shape Changed using C#


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.

// Find and replace text using regular expressions
FindReplaceOptions fo = new FindReplaceOptions(doc);
fo.RegularExpressions = true;
doc.Body.Replace("\\$[0-9]+", "$50000", fo);

Below is the resulting document generated after executing the above-defined code to find and replace text.

Find and Replace Text In Word Using C#

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.

//Add a hyperlink
var par = doc.Body.Paragraphs[18];
par.GetRange().Runs[0].Delete();
Hyperlink wlink = par.GetRange().Hyperlinks.Add(new Uri("http://www.example.com"), "", "Click to go to website.");

With the code above implemented, notice the updated DOCX files now have a hyperlink.

Add a hyperlink to MS Word DOCX C#


Save the Updated Word .DOCX Document using C#

Save the server-side MS Word document using the APIs Save method of the DsWordDocument class.

//Save the created Word file
doc.Save("UpdatedDoc.docx");

Notice the updated and saved .NET DOCX file now contains the new image, updated price, and a hyperlink to the company's website.

Updated DOCX file using .NET Word API


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!

Tags: