.NET API for Manipulating Word Processing Documents

Open Source .NET Library to Create, Edit, Split or Join & Manage Custom Headers/Footers in Microsoft Word files.

DocX is an open source pure .NET library that enables software developers to create and manipulate Word 2007/2010/2013 files, in an easy manner. It is a lightweight and very fast API that does not use COM libraries nor does it require Microsoft Office to be installed. The DocX API makes it easy to create and manipulate documents.

The API allows adding headers or footers to word documents. It can be the same on all the pages, or can be unique on the first page, or unique for odd or even word document pages. The great thing is that it can contain images, hyperlinks, and more.

The API supports several important features such as applying a template to a Word document, modifying word documents, joining documents, recreating portions from one to another, document protection with or without a password, setting document margins, setting page size, line spacing, indentation, text direction, text alignment, manage fonts and font sizes, create sections and many more.

Previous Next

Getting Started with DocX

First of all, you need to install .NET framework 4.0 and Visual Studio 2010 or later, in order to use DocX smoothly. 

The recommended way to install is using NuGet

Install DocX from NuGet

 Install-Package DocX -Version 1.5.0

Create & Modify Word Documents via .NET APIs

The open source DocX API allows software developers to create new Word documents in DOCX file formats. The library also allows modification of the existing Word Documents according to the user’s needs. The API also supports adding a picture, inserting a table and hyperlinks, and adding paragraphs or bulleted lists and a numbered list inside word documents.

Create DOCX using DocX - C#

using (DocX document = DocX.Create("fileformat.docx"))
{
  // Add a new Paragraph to the document.
  Paragraph pagagraph = document.InsertParagraph();
  // Append some text.
  pagagraph.Append("File Format Developer Guide").Font("Arial Black");
  // Save the document.
  document.Save();
}

Manage Custom Headers/Footers in Word Files

The DocX API enables Software developers to add custom headers and footers inside Word DOCX Documents. The API supports adding custom headers/footers with tables and pictures, paragraphs, and charts. Once the document is created you can define the page's header's picture in a Table. Odd and even pages will have the same headers. You can also describe the page's footer's picture or text in a Table.

Create Header and Footer - C#

//Create a document
using (DocX document = DocX.Create("FileFormat.docx"))
{
  // Add Header 
  document.AddHeaders();
  Header header = document.Headers.Odd;
  // Insert Paragraph in header
  Paragraph paragraph = header.InsertParagraph();
  paragraph.Append("File Format Developer Guide");
  // Add Footer 
  document.AddFooters();
  Footer footer = document.Footers.Odd;
  // Insert Paragraph in header
  Paragraph paragraph1 = footer.InsertParagraph();
  paragraph1.Append("File Format Develoer Guide");
  // Save Document
  document.Save();
}

Manipulate Image inside a Word Document

The DocX API allows software developers to programmatically manipulate images embedded inside Word DOCX documents. First of all, you need to have an image inside a word document. To manipulate the image first you need to open the document and write a custom string or other changes you want to apply to an image and after that save the document.

Manipulate Images using DocX - C#

using (DocX document = DocX.Load("FileFormat.docx"))
{
  //Read Image from word
  var image = document.Images[0];
  Bitmap bitmap = new Bitmap(image.GetStream(FileMode.Open, FileAccess.ReadWrite));
  Graphics graphics = Graphics.FromImage(bitmap);
  // Draw the string
  graphics.DrawString
  (
  "Manipulate Images using DocX",
  new System.Drawing.Font("Tahoma", 20),
  Brushes.Blue,
  new PointF(0, 0)
  );
  // Save document
  document.SaveAs("Output.docx");
}

Manage Hyperlinks in Word Documents

The hyperlink is a very useful part of word processing documents that allows users to access external documents or internal parts of the existing documents as well as external websites and email addresses directly from the document. The open source DocX API provides support for both two types of hyperlinks; the internal which points to a bookmark inside the document and the external which points to the external URL.

Manage Hyperlinks Word Processing Documents via C#


// reating a bookmark
const chapter1 = new Paragraph({
  heading: HeadingLevel.HEADING_1,
  children: [
    new Bookmark({
      id: "anchorForChapter1",
      children: [
        new TextRun("Chapter 1"),
      ],
    }),
  ],
})

//Create an hyperlink 

const link = new InternalHyperlink({
  children: [
    new TextRun({
      text: "See Chapter 1",
      style: "Hyperlink",
    }),
  ],
  anchor: "anchorForChapter1",
})

 English