C# .NET Word Automation – Create, Edit or Convert MS Word Documents Programmatically

C# .NET Word Automation

Are you looking for a .NET Word Automation solution to generate and process word processing documents in C#? Do you want to create, read, edit/modify and convert Word documents programmatically without requiring MS Office? Let’s try out Aspose.Words for .NET API – A complete set of features for generation and processing of Word documents (.doc, .docx, etc.) in your .NET or .NET Core based applications.

MS Word document automation and report generation are in high demand by enterprises. Aspose.Words for .NET is a complete and feature-rich Word automation solution to create, edit or analyze Word documents in such cases. This article covers all the basic features required for generating and manipulating the Word documents programmatically using C#. Once you read this article, you will be able to:

  • Create a Word (.doc, .docx) document.
  • Edit or modify a Word document.
  • Convert a Word document to other formats such as PDF.
  • Parse a Word document.

Aspose.Words for .NET – C# Word Automation API

Aspose.Words for .NET DLL can be downloaded from here. Another option is to install it via NuGet Package Manager or Package Manager Console in Visual Studio.

Using NuGet Package Manager

C# .NET Word Automation Library

Using Package Manager Console

PM> Install-Package Aspose.Words

Create a Word Document Dynamically using C#

Let’s start our journey by creating a new Word document using Aspose.Words for .NET. The DocumentBuilder class of the API contains all the methods and properties to create Word documents from scratch. In combination with the Document class, DocumentBuilder supports inserting elements such as text/paragraphs, checkboxes, tables, lists, images and the other objects that a Word document can contain. Moreover, you can specify the font and other formatting options using this class. The following are the steps to create a Word document using DocumentBuilder class.

  • Create a new Document object.
  • Create and new DocumentBuilder object and initialize it with the Document object.
  • Insert/write elements using the DocumentBuilder object.
  • Save the document using Document.Save method.

The following code sample shows how to create a Word DOCX document with formatted text, table, and image in C#.

The following is the document we get after executing the above code.

Create Word document in C# .NET

Visit these articles to explore more about the elements you can add to a Word document using Aspose.Words for .NET.

Edit or Modify a Word Document in C#

You can also edit or modify the existing Word documents using Aspose.Words for .NET. Aspose.Words Document Object Model classes let you access and modify the document’s elements as well as their formatting. DOM is actually the in-memory representation of the document. To understand the document representation as DOM, please see the DOM overview.

Let’s now check out how to edit an existing element in a Word document. Suppose we need to update the text “This is the first page.” in the document that we have recently created. Since this is the first paragraph in the first section of the document, we can access it by specifying the index of the first section and the first paragraph. The following code sample shows how to edit the paragraph in the Word document using C#.

The following is the updated Word document.

Word Automation in C# .NET

Convert Word Document to Other Formats in C#

Along with creating and manipulating Word documents, Aspose.Words for .NET also allows converting a document to other formats including (but not limited to) PDF, XPS, EPUB, HTML, and image formats such as BMP, PNG or JPEG. The following code sample shows how to convert a Word document to PDF in C#.

Visit this article to see how to convert a Word document to other formats.

Parse a Word Document in C#

You can also parse a Word document by extracting its content as a plain text. The following code sample shows how to extract text from a Word document and save it into a .txt file.

Visit the documentation of Aspose.Words for .NET for a complete Developer’s Guide. In case you would find anything confusing for you, do let us know via our forum.

Related Article(s)