Free .NET Library for Creating Word Processing Documents
Read, Write, Manipulate & Convert Word files via Open Source .NET API.
NetOffice is an open source API that allows .NET developers to automate Microsoft Office and develop Microsoft Office Add-ins. Using the API, the developer can use all the features included in MS Office versions 2000, 2002, 2003, 2007, 2010, 2013, and 2016. The API is based on COM-architecture where you retrieve COM proxy objects in your application and you have to free every COM proxy object. The main advantage of using NetOffice is the ability to work with a different version of Microsoft Office at a single time and it offers a protection mechanism for the management of COM proxies.
Using the API, you can develop an application for both 32-bit and 64-bit operating systems. You can use the NetOffice assemblies in all scenarios without any worries. The API provides a bunch of libraries to manipulate office documents. In order to work with Microsoft Word documents, you need to WordApi.dll with OfficeApi.ddl, VBIDEApi.dll, and NetOffice.dll as dependencies.
Getting Started with NetOffice
First of all, you require to have .NET Framework 4.5 or above. After that, please download the repository manually from GitHub or install it from NuGet.
Installation NetOffice from NuGet
Install-Package NetOfficeFw.Word
Create Word Document using Free C# API
NetOffice allows .NET programmers to create Microsoft Word Files programmatically. In order to create and word file, first, you need to initialize a Word application and turn off message boxes. After your word application is started you can add a new document to it using WordApplicaiton.Documents.Add() method. You can insert text in your newly created word file using WrodApplication.Selection.TypeText() method and set font using WordApplicaiton.Selection.Font() method. Once you are done with your document, you can save it using Document.SaveAs() method.
Create Word Document via NetOffice Library
// start word and turn off msg boxes
Word.Application wordApplication = new Word.Application();
wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
// create a utils instance, no need for but helpful to keep the lines of code low
CommonUtils utils = new CommonUtils(wordApplication);
// add a new document
Word.Document newDocument = wordApplication.Documents.Add();
// insert some text
wordApplication.Selection.TypeText("This text is written by automation");
wordApplication.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend);
wordApplication.Selection.Font.Color = WdColor.wdColorSeaGreen;
wordApplication.Selection.Font.Bold = 1;
wordApplication.Selection.Font.Size = 18;
// save the document
string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example01", DocumentFormat.Normal);
newDocument.SaveAs(documentFile);
// close word and dispose reference
wordApplication.Quit();
wordApplication.Dispose();
// show end dialog
HostApplication.ShowFinishDialog(null, documentFile);
Create a Table in Word using C#
NetOffice allows .NET programmers to add Tables in Microsoft Word File programmatically. In order to add tables in a Word file first, you need to initialize a Word application and turn off message boxes and add new documents using WordApplicaiton.Documents.Add() method. You can insert a table in your newly created word file by initializing Word.Table and set table range using Tables.Add(wordApplication.Selection.Range, 3, 2) method. You can insert text in your cells by selecting a specific cell using table.Cell(1, 1).Select() and insert text in it. Once you are done with your document, you can save it using Document.SaveAs() method.
Create Table in Word File via .NET
Word.Application wordApplication = new Word.Application();
wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
// create a utils instance, not need for but helpful to keep the lines of code low
CommonUtils utils = new CommonUtils(wordApplication);
// add a new document
Word.Document newDocument = wordApplication.Documents.Add();
// add a table
Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 3, 2);
// insert some text into the cells
table.Cell(1, 1).Select();
wordApplication.Selection.TypeText("This");
table.Cell(1, 2).Select();
wordApplication.Selection.TypeText("table");
table.Cell(2, 1).Select();
wordApplication.Selection.TypeText("was");
table.Cell(2, 2).Select();
wordApplication.Selection.TypeText("created");
table.Cell(3, 1).Select();
wordApplication.Selection.TypeText("by");
table.Cell(3, 2).Select();
wordApplication.Selection.TypeText("NetOffice");
// save the document
string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example02", DocumentFormat.Normal);
newDocument.SaveAs(documentFile);
// close word and dispose reference
wordApplication.Quit();
wordApplication.Dispose();
// show end dialog
HostApplication.ShowFinishDialog(null, documentFile);