create MS Word Documents in C#

create MS Word Documents in C#

Microsoft Word automation to create and edit documents programmatically may not be a difficult task now. Manipulating the existing MS files using a few lines of source code in C# may give you a competitive edge in your business software. However, in this blog post, we are going to learn how to create DOCX MS Word Documents in C#. This .NET library offers a wide range of methods and properties to perform various tasks regarding Word automation.

The following points shall be covered in this tutorial:

Create DOCX MS Word Documents in C# - API Installation

You can install this .Net API in two ways.

Download the DLL files to enable this library in your .NET application.

Or, you may install it by running the following command in the NuGet package manager.

Install-Package Aspose.Words

How to Create a Word Document DOCX Programmatically in C#?

After the successful installation of the .NET Word automation API, we are all set to write code in C# to create our first MS Word document programmatically in C#.

You can follow the following steps and the code snippet to create a DOCX Word Document in C#.

  1. Create an object of the Document class to create a blank MS Word file.
  2. Initialize an instance of DocumentBuilder class and attach it to a new Document object.
  3. Invoke the Write method to insert a string into the document.
  4. Call the save method to save the MS Word file.

The code snippet below shows how to create a DOCX Word document in C#:

Add Paragraphs in a Word File using C#

In this section, we will learn how to add paragraphs in an MS Word DOC programmatically in C#.

The following steps and the code snippet can be followed:

  1. Instantiate an object of the Document class to create a blank MS Word file.
  2. Create an instance of DocumentBuilder class and attach it to a new Document object.
  3. Call the Writeln method to insert a paragraph break into the document.
  4. Writeln(string) method adds a paragraph.
  5. The save method will save the MS Word file.

The sample code below explains how to insert paragraphs in a Word document using C#:

How to Insert a Table in an MS Word File in C#

Follow the steps and the code snippet to add a table in a Word DOC using C#:

  1. Initialize an instance of the Document class to create a blank MS Word file.
  2. Create an instance of DocumentBuilder class and attach it to a new Document object.
  3. Call the StartTable method to insert a table into the MS Word file.
  4. InsertCell method will insert a table cell.
  5. The EndRow method ends a row of a table.
  6. Invoke the EndTable method to end the table.
  7. Cal the save method to save the MS Word file.

The following code sample demonstrates how to insert a table in a Word document using C#:

Adding a List in a Word DOC Programmatically in C#

The list is an integral part of any document. However, you can add the list component in a Word document in C# by following the steps and the code snippet mentioned below:

  1. Create an object of the Document class to create a blank MS Word file.
  2. Instantiate an instance of DocumentBuilder class and attach it to a new Document object.
  3. Call ListFormat property that returns an object that represents current list formatting properties and the ApplyBulletDefault method will apply an indent and a bullet symbol ("•") before each paragraph.
  4. Invoke the save method to save the MS Word file.

The code sample below demonstrates how to add a list in a Word DOCX DOC document using C#:

How to Edit the Fonts of a Word Document in .NET applications?

Now, we will add, and edit the fonts in an MS Word DOC using C# code programmatically.

Follow the following steps and the code snippet:

  1. Create an object of the Document class to create a blank MS Word file.
  2. Initialize an instance of DocumentBuilder class and attach it to a new Document object.
  3. Font class offers various properties to style the font of a Word document.
  4. LineWidth property sets the border width in points.
  5. LineStyle specifies the line style of a border.
  6. Call the Size property to set the size of the font.
  7. The Name property is used to set the name of the font.
  8. Make your text bold by calling Bold property.
  9. Call the save method to save the MS Word file.

The code snippet below elaborates on editing the fonts in a Word document using C#:

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Summing up

The following output.docx will be generated upon running the above code snippets altogether in a single C# file.

create MS Word Documents in C#

This brings us to the end of this blog post. We have demonstrated how to create MS Word documents in C# programmatically. We also have explored how to add various components to an MS Word file such as Tables, Lists, Paragraphs and Fonts. In addition, you can go through the other methods by visiting the documentation of this .NET API. We will write on other useful topics in future blog posts. Therefore, stay in touch with conholdate.com for updates.

Ask a question

You can let us know about your questions or queries on our forum.

See Also