How to Programmatically Generate Word DOCX in C# .NET
Tags: webdev, devops, csharp, tutorial
Published: 2024-05-31 17:49:03

---
canonical_url: https://developer.mescius.com/blogs/how-to-programmatically-generate-word-docx-in-c-net
description: Learn how to programmatically generate Word DOCX in C# .NET. See more from Document Solutions today.
---
**What You Will Need**
- [Document Solutions for Word](https://developer.mescius.com/document-solutions/dot-net-word-api/download)

**Controls Referenced**
- [Document Solutions for Word (DsWord) - C# .NET Word API](https://developer.mescius.com/document-solutions/dot-net-word-api)
- [Documentation](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/overview.html) | [Online Demo Explorer](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/)

**Tutorial Concept**

Programmatically load and modify a DOCX file using a .NET C# Word API. Learn how to update an image in the file, find and replace text, and add hyperlinks in your .NET server-side application.

---
[DsWord](https://developer.mescius.com/document-solutions/dot-net-word-api "https://developer.mescius.com/document-solutions/dot-net-word-api") API is a feature-rich library that enables you to work with Word documents through code on multiple platforms, such as Windows, macOS, and Linux. The feature set is vast enough to create, edit, save, merge, and even split Word documents. The capabilities do not end with these basic operations but include working with images, tables, OMath, Shapes, Lists, Text, and many more. The feature-rich object model of the API is based on Microsoft Office API. As a result, the documents generated using DsWord can easily be viewed and manipulated in MS Word. 

All of these features can be explored in greater detail through the [documentation](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/overview.html") and [demos](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/ "https://developer.mescius.com/document-solutions/dot-net-word-api/demos/") available on the website.

For this blog, we will focus on a specific use case scenario - modifying a flyer. We will use some features of the DsWord API to generate a new and improved version of this flyer document. This will help you get started with the API and explore the most basic class of the API, **GcWordDocument**,which represents the Word document in code.

We will be exploring some of the features of the DsWord API as listed below while we update an existing Word document. We will use a real estate flyer as an example, which we will modify using the steps below:

*   [Load an Existing Word Document](#Load)
*   [Update an Image Contained in a Shape](#Update)
*   [Find and Replace Text Using Regular Expressions](#Find)
*   [Add a Hyperlink](#Add)

Below is a quick view of the flyer along with its updated version, highlighting all changes within red borders. We will implement these changes using the DsWord API in the steps that follow.

![Updated flyer](//cdn.mescius.io/umb/media/zdllblmj/updated-flyer.png?rmode=max&width=756&height=466)

For convenience, we have created a basic Windows form application with simple controls, such as TabControl, TreeView, TextBox, and buttons. This application provides a UI with a list of actions that, when selected, show the respective code needed to perform that action using the DsWord API. Clicking on the Execute Code button in the application, you can execute the code and generate the expected Word document.

Below is a preview of the application UI:

![App UI](//cdn.mescius.io/umb/media/p5umuu3y/app-ui.png?rmode=max&width=754&height=459)

Now, let’s use this application and explore various code snippets to implement the listed features, then view the final document that results.

## <a id="Load"></a>Load an Existing Word Document

We will begin by loading an existing Word document. The code below loads a flyer document into an instance of the [**GcWordDocument**](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.GcWordDocument.html?highlight=gcworddocument%2C "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.GcWordDocument.html?highlight=gcworddocument%2C") class. You can explore the documentation to gain more insight into the [loading and creation](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/quickstart.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/quickstart.html") of Word documents.

The document will then be saved at a local path for further processing as we move forward.

The image below depicts the demo app, which is showcasing the code used to create the document with all the defined elements:

![Demo App](//cdn.mescius.io/umb/media/klvflsn3/demo-app.png?rmode=max&width=754&height=451)

The document below is generated after executing the above-defined code:

![Resulting Doc](//cdn.mescius.io/umb/media/zqjffyfp/resulting-doc.png?rmode=max&width=732&height=805)

## <a id="Update"></a>Update an Image Contained in a Shape

The DsWord API enables you to add different shapes to a Word document, ranging in type from basic lines and rectangles to more complex shapes like clouds and donuts, etc. Further, it facilitates the enhancement of these shapes by supporting different fill types, including PatternFill, SolidFill, GradientFill, and ImageFill. Refer to the [Shape](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/shape.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/shape.html") and [ShapeFormat](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/shape-format.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/shape-format.html") documentation for a more thorough understanding of the Shapes feature.

The flyer document in this blog showcases the ImageFill feature, which allows the user to fill a shape with an image to be displayed in the flyer. In this section, we will explore how to update the added image using the DsWord API. You can access the image existing in the shape by using the [ImageFill](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FillFormat~ImageFill.html?highlight=imagefill%2C "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FillFormat~ImageFill.html?highlight=imagefill%2C") property of the [FillFormat](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FillFormat.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FillFormat.html") class. Once we have the current image data, we can replace it by invoking the [SetImage](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.EmbeddedImageData~SetImage(Uri,String).html?highlight=setimage%2C "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.EmbeddedImageData~SetImage(Uri,String).html?highlight=setimage%2C") method of the [EmbeddedImageData](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.EmbeddedImageData.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.EmbeddedImageData.html") class, which sets a new image for the Shape’s ImageFill type.

The image below depicts the demo app showcasing the code used to update an image in a shape within the document:

![ImageFill Shape](//cdn.mescius.io/umb/media/hxpmcn3u/imagefill-shape.png?rmode=max&width=742&height=445)

Below is the resulting document that is generated after executing the above-defined code:

![ImageFill Shape Doc](//cdn.mescius.io/umb/media/3egl40ki/imagefill-shape-doc.png?rmode=max&width=744&height=794)

Visit these online [demos](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/shapes "https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/shapes") to explore formatting shapes more.

## <a id="Find"></a>Find and Replace Text Using Regular Expressions

Finding and replacing text is the most commonly required capability when working with text documents, so this section will demonstrate how this can be accomplished using DsWord. We will use the [Find](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.RangeBaseFindReplaceExtensions~Find.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.RangeBaseFindReplaceExtensions~Find.html") and [Replace](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.RangeBaseFindReplaceExtensions~Replace.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.RangeBaseFindReplaceExtensions~Replace.html") methods and will explore different options that can be set for either of these operations to customize it as per user requirements. Refer to the [Text](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/text.html#i-heading-find-and-replace-text "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/text.html#i-heading-find-and-replace-text") documentation for more details on the implementation of this feature.

For this example, we will replace the house price listed on the flyer, which is one variable that changes over time and would require updating. Because the value is ever-changing, replacing it as text with a new constant value every time would be difficult. Hence, we opted to use regular expressions to find the value to be replaced, as it will always appear as a numerical value preceded by a dollar symbol. 

DsWord allows you to use a regular expression as a search string, which is typically referred to as a find pattern. To ensure the Find and Replace methods treat the provided search string as a regular expression, we must set the [RegularExpressions](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindOptions~RegularExpressions.html?highlight=regularexpressions%2C "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindOptions~RegularExpressions.html?highlight=regularexpressions%2C") property to true either for the [FindOptions](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindOptions.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindOptions.html") class or for the [FindReplaceOptions](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindReplaceOptions.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.FindReplaceOptions.html") class, depending on whether we are invoking Find or Replace methods respectively. The regular expression used in the code below is a pattern featuring a dollar symbol and numerical range, which searches for a dollar sign followed by one or more numbers ranging from 0 to 9.

The image below depicts the demo app showcasing the code used to find and replace the house listing price in the document using regular expressions:

![Find Replace](//cdn.mescius.io/umb/media/1yjprjqx/find-replace.png?rmode=max&width=717&height=430)

Below is the resulting document that is generated after executing the above-defined code:

![Find Replace Doc](//cdn.mescius.io/umb/media/n1qne3o0/find-replace-doc.png?rmode=max&width=732&height=800)

Visit our online [demos](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/find-replace "https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/find-replace") to explore the different Find and Replace approaches one can implement using the DsWord API.

## <a id="Add"></a>Add a Hyperlink

Flyers generally provide a website link for customers so they can find more detailed information regarding the product being promoted. While this flyer also provides a website link, it has been added as text. However, in today’s digital era, it is likely that this flyer will be distributed virtually. In that case, adding the website address as a hyperlink would be much more convenient than text, as it will allow the user to click on the link directly and navigate to the website rather than copying and pasting the link to access it.

In this section, we will update the website address to be added as a hyperlink using the [Hyperlink](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.Hyperlink.html?highlight=hyperlink%2C "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/DS.Documents.Word~GrapeCity.Documents.Word.Hyperlink.html?highlight=hyperlink%2C") class provided by DsWord.

Refer to the [Links](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/links.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/links.html") documentation for more details on the API features depicted in the code below.

The image below depicts the demo app showcasing the code used to add a hyperlink to the document:

![Add Hyperlink](//cdn.mescius.io/umb/media/wbuddg1e/add-hyperlink.png?rmode=max&width=739&height=444)

Below is the resulting document that is generated after executing the above-defined code:

![Add Hyperlink Doc](//cdn.mescius.io/umb/media/fcndrbnd/add-hyperlink-doc.png?rmode=max&width=741&height=797)

Visit our online demos to explore how to add [hyperlinks](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/content/hyperlinks/word-cs "https://developer.mescius.com/document-solutions/dot-net-word-api/demos/features/content/hyperlinks/word-cs") to your Word document using the DsWord API.

Download [the demo sample](https://cdn.mescius.io/umb/media/k5kdvjlw/processworddoc.zip) from this blog to modify and generate a sample Word document as in this tutorial.

Refer to our [documentation](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/overview.html") and [demos](https://developer.mescius.com/document-solutions/dot-net-word-api/demos/ "https://developer.mescius.com/document-solutions/dot-net-word-api/demos/") for comprehensive guidance and practical examples of DsWord features, enabling efficient and error-free implementation.