DEV Community

Cover image for How to Programmatically Generate Word DOCX in C# .NET
Chelsea Devereaux for MESCIUS inc.

Posted on • Originally published at developer.mescius.com

How to Programmatically Generate Word DOCX in C# .NET

What You Will Need

Controls Referenced

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 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 and 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:

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

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

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

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 class. You can explore the documentation to gain more insight into the loading and creation 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

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

Resulting Doc

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 and ShapeFormat 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 property of the FillFormat class. Once we have the current image data, we can replace it by invoking the SetImage method of the EmbeddedImageData 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

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

ImageFill Shape Doc

Visit these online demos to explore formatting shapes more.

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 and Replace 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 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 property to true either for the FindOptions class or for the FindReplaceOptions 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

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

Find Replace Doc

Visit our online demos to explore the different Find and Replace approaches one can implement using the DsWord API.

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 class provided by DsWord.

Refer to the Links 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

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

Add Hyperlink Doc

Visit our online demos to explore how to add hyperlinks to your Word document using the DsWord API.

Download the demo sample from this blog to modify and generate a sample Word document as in this tutorial.

Refer to our documentation and demos for comprehensive guidance and practical examples of DsWord features, enabling efficient and error-free implementation.

Top comments (0)