Are you searching for a solution to automate Microsoft Word features, i.e. creating, updating, and converting Word documents? If yes, this article is going to make your life easier by demonstrating how to create Word documents in C# without MS office. In addition, you will learn how to edit an existing Word document, convert a DOC/DOCX file, or parse a document in a few simple steps. You will also get an easy-to-integrate code sample to implement each feature.
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 Word documents programmatically using C#. Once you read this article, you will be able to:
- C# Library to Automate MS Word
- Create a Word (.doc, .docx) document using C#
- Edit or modify a Word document using C#
- Convert a Word document using C#
- Parse a Word document using C#
C# Library to Automate MS Word
Aspose.Words for .NET is an amazing library with a range of features to create and manipulate MS Word documents. Make sure to install this library into your application before creating a new Word document. You can download the library from here. Another option is to install it via NuGet Package Manager or Package Manager Console in Visual Studio.
PM> Install-Package Aspose.Words
How to Create a Word Document in C#
Let’s start our journey by creating a new Word document from scratch.
The DocumentBuilder class of the library contains all the methods and properties to create Word documents. 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 simple steps to create a new Word document in C#.
- 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 in C#.
The following is the document we get after executing the above code.
Visit these articles to explore more about the elements you can add to a Word document using Aspose.Words for .NET.
Edit a Word DOC 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 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.
C# Microsoft Word Automation - Convert a Word Document
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.
Automate Word Document Parsing in C#
You can also parse a Word document by extracting its content as plain text. The following code sample shows how to extract text from a Word document and save it into a .txt file.
Create MS Word Documents with a Free License
You can get a free temporary license and create MS Word documents from scratch without evaluation limitations.
C# Word Automation Library - Read More
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.
There are various scenarios where Microsoft Word automation becomes useful. For example, for generating reports, invoices, etc. This article provides the easiest solution of how to create MS Word documents in C# within a few simple steps. Moreover, you have learned how to use C# Word automation library to edit, parse and convert Word documents.
- Convert Word Documents to PDF using C# - A complete guide.