Skip to Main Content
Basics of creating a docx with Words for .NET

Basics of creating a docx with Words for .NET

Learn more about Xceed Words for .NET

 

To get started, creating a new Word document with Xceed Words for .NET needs very little.

Let us start at the very beginning, and simply create a blank document. All we need is to specify the filename (or file path and name), then save the document:

// 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 documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple