Engineered for platform independence and convenience, GrapeCity Documents for Word (GcWord) is a high-performance Word API that offers a complete solution to work with Word documents in .NET Standard 2.0 applications. Generate, load, edit, and save Word documents in .NET Standard 2.0 with no dependencies on Microsoft Word! With a rich, intuitive object model, GcWord is easier to use than Microsoft’s API.
In another article, we discuss how to get started with Documents for Word on Windows, Mac and Linux. This blog guides you through how to use GcWord to create Word documents in code.
GcWord can be used in any application that targets .NET Standard 2.0 (including .NET Core, .NET FrameWork, Mono and Xamarin).
dotnet new console
You can install the GcWord NuGet package using Visual Studio or the dotnet CLI.
dotnet add package GrapeCity.Documents.Word
using GrapeCity.Documents.Word;
In the Program.cs file's Main method, paste the following line of code between the braces. This creates a new document by calling the GcWordDocument class.
GcWordDocument doc = new GcWordDocument();
We'll use the GcWord object model to create sections of a Word document. Before we start, let's look at the GcWord architecture.
The main body of a Word document is split into different sections, and you can add paragraphs to different sections of the document. You can access the sections using doc.Body.Sections.
To add a title, first, get the range of the first section. Then write the code to add a paragraph that contains title text to the first section of the document. In addition, set the alignment of the title to the center of the document.
// Add the first title to the document:
// The primary way of manipulating objects in GcWord is via ranges.
//Get the range of first section of the document body in which we will add title to the document.
Range range = doc.Body.Sections.First.GetRange();
Paragraph p = range.Paragraphs.Add("Introduction");
p.Format.Alignment = ParagraphAlignment.Center;
Range r = p.GetRange();
Run run = r.Runs.First;
run.Font.Size = 20;
run.Font.Name = "Arial";
p = range.Paragraphs.Add("The Importance of Wetlands");
p.Format.Alignment = ParagraphAlignment.Center;
r = p.GetRange();
run = r.Runs.Last;
run.Font.Size = 16;
run.Font.Name = "Arial";
Add the following code to set the style for the rest of the paragraphs in the document:
var pStyle = doc.Styles.Add("par style 1", StyleType.Paragraph);
pStyle.ParagraphFormat.Indentation.FirstLineIndent = 36;
pStyle.ParagraphFormat.Alignment = ParagraphAlignment.Distribute;
This code sets the character style for characters in the rest of the document:
var cStyle = doc.Styles.Add("char style 1", StyleType.Character);
cStyle.Font.Size = 9;
The code gets the paragraphs from a text file and adds them to the paragraph object of GcWord. Then it sets similar properties to align the paragraphs as "Distribute" and sets the font size to a smaller font:
//Get text for paragraphs from a text file
string line;
int counter = 0;
System.IO.StreamReader file = new System.IO.StreamReader(@"TheImportanceOfWetlands.txt");
//Add a paragraph and set paragraph style on it
p = doc.Body.Sections.First.GetRange().Paragraphs.Add();
p.Style = pStyle;
//Read the text file line by line and add to the last run in the document
while ((line = file.ReadLine()) != null && line != @"\n")
{
if (string.IsNullOrEmpty(line))
{
p = doc.Body.Sections.First.GetRange().Paragraphs.Add();
p.Style = pStyle;
}
run = p.GetRange().Runs.Add(line);
run.Style = cStyle;
counter++;
}
Save the document to .docx file:
doc.Save("ImportanceOfWetlands.docx", DocumentType.Document);
Your document looks like this:
What do you think about using GrapeCity's Word library in .NET applications? Do you have any special use cases? Leave us a comment below!