Creating Word Documents Programmatically with Xceed Words for .NET in C#

If you want to create Word documents programmatically using C#, Xceed Words for .NET is a powerful and easy-to-use API that can help you get the job done. In this article, we’ll explore how to use Xceed Words for .NET to create a new Word document and add content to it.

Learn more about Xceed Words for .NET

If you want to create Word documents programmatically using C#, Xceed Words for .NET is a powerful and easy-to-use API that can help you get the job done. In this article, we’ll explore how to use Xceed Words for .NET to create a new Word document and add content to it.

Creating a New Word Document

To create a new Word document programmatically in C# using Xceed Words for .NET, you simply need to specify the filename or file path and name, then save the document. This process can be achieved with just a few lines of code, as shown in the example below:

// Create a new document
	using( var document = DocX.Create( "NewDocument.docx" ) )
	{
		// Save this document to disk.
		document.Save();
	}

We can also open an existing document instead of creating a new one (by using Load instead of Create), and we can choose to save to a new document instead of overwriting the existing one (by using SaveAs instead of Save):

// Open an existing document
	using( var document = DocX.Load( "ExistingDocument.docx" ) )
	{
		// Save this document to disk.
		document.SaveAs( "NewCopy.docx" );
	}

Notes:

  • The Create, Load and SaveAs methods expect either a String or Stream as parameter.
  • The Create method also supports an optional parameter to indicate the document type.
  • For more information please refer to the documentation.

Adding Paragraphs

A blank document is not very useful, so we will add a few paragraphs.

Adding a paragraph is done by calling InsertParagraph on the Document. This will return the newly created paragraph, so we can assign it to a variable if we plan on doing any modifications to it.

// Insert a blank paragraph.
document.InsertParagraph();

// Insert a paragraph that we want to modify.
var p = document.InsertParagraph();

Once we have a paragraph in hand, we can modify it in several ways. We can append elements such as text, bookmark, hyperlink, picture, etc. We can also change the format, such as the font family, font size, color, make it bold/italic/underlined, etc.

For this time, we will focus on appending simple text, and looking at a few ways that we can customize how that text looks.

Appending text to a paragraph

Adding text can be done by calling Append on the paragraph. If the text we append should have a certain look, that can also be specified at the same time.

Here are a few examples:

// Append text without any formatting
var p1 = document.InsertParagraph();
p1.Append(“This is the first paragraph.”);

// Append text formatted to be Red and Bold
var p2 = document.InsertParagraph();
p2.Append(“This is the second paragraph.”)
.Color( Color.Red )
.Bold();

// Append text formatted to be in Arial and size 24
var p3 = document.InsertParagraph();
p3.Append(“This is the third paragraph.”)
.Font( new Xceed.Document.NET.Font( "Arial" ) )
.FontSize( 24 );

We can also append more than one text block at the same time and have each with their own formatting:

// Append text formatted to be Red and Bold
var p = document.InsertParagraph();
p.Append(“This is a simple formatted red bold paragraph”)
.Font( new Xceed.Document.NET.Font( "Arial" ) )
.FontSize( 24 )
.Color( Color.Red )
.Bold()
.Append(“ containing a blue italic text.”)
.Font( new Xceed.Document.NET.Font( "Times New Roman" ) )
.Color( Color.Blue )
.Italic();

Notes:

  • The Append method also supports an optional parameter to indicate the Formatting.
  • This Formatting parameter allows you to set a common format and re-use it more easily.
  • For more information please refer to the documentation.

There you have it, your first steps in creating a Words document (docx) using C#. We will have more articles available soon to help you learn more advanced functionalities.

For more information, please refer to the la documentation.