To automate the manipulation of documents within our applications we need some reliable APIs. The market offers both Open Source Software (OSS) and Closed Source Softwares (CSS) to work with Word Processing Documents. Closed source APIs are often costly. There are a bunch of free APIs available with both basic and advanced features, following are a few of them:
Getting Started with Free APIs
Let’s get started with the installation and basic usage of APIs.
Open XML SDK
Open XML SDK requires .NET Framework 3.5 or above. You can install the library from NuGet using the following command.
Install-Package DocumentFormat.OpenXml
After you are done with the installation, you can create a simple DOCX document free using the following code.
// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add paragraph
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("File Format Developer Guide"));
}
For details please visit this link.
NPOI
NPOI is a .NET version of the POI Java Project. Just like Open XML SDK, you can install in using NuGet.
Install-Package NPOI -Version 2.4.1
Similarly, creating a document with NPOI is even simpler. You can create a DOCX file using a few lines of code.
using (FileStream sw = File.Create("fileformat.docx"))
{
XWPFDocument doc = new XWPFDocument();
doc.CreateParagraph();
doc.Write(sw);
}
For details please visit this link.
FileFormat.Words for .NET
Using FileFormat.Words for .NET, you can manipulate Word 2007/2010/2013 files easily. To get started with FileFormat.Words for .NET, you can install it using.
Install-Package FileFormat.Words
Like Open XML SDK & NPOI, creating a document with DocX is pretty simple
// Create an instance of the Document class.
using (Document doc = new Document())
{
//Initialize the constructor with the Document class object.
Body body = new Body(doc);
// Instantiate an instance of the Paragraph class.
Paragraph para1 = new Paragraph();
// Set the text of the paragraph.
para1.AddRun(new Run { Text = $"This is a Paragraph." });
// Invoke AppendChild method of the body class to add a paragraph to the document.
body.AppendChild(para1);
// Call the Save method to save the Word document onto the disk.
doc.Save("/Docs.docx");
}
For details please visit this link.