GrapeCity Documents for Word (GcWord) is a Word API offering a complete solution to work with Word documents in .NET Standard 2.0 applications. In this tutorial, we'll build a .NET Core console application with GcWord in Visual Studio for Windows and MAC, and Visual Studio Code for Linux. We'll create a program to generate (and save to disk) a Word document with the "Hello, World!" text.
This adds the required references to your application. Jump to step 2 to add code in the application and generate a Word document.
This adds the required references to your application. To generate a Word document, jump to step 2.
$ mkdir ~/MyApp # create a directory for the application
$ cd ~/MyApp
$ dotnet new console # create a .NET Core application with MyApp.csproj and Program.cs files
This adds a reference to that package in your .csproj file. It looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GrapeCity.Documents.Word" Version="2.0.0.*" />
</ItemGroup>
</Project>
In a terminal window, type the following commands to build and run the app:
$ cd ~/MyApp
$ dotnet restore # fetches referenced packages for MyApp.csproj
$ dotnet run # runs the default app
At this point, you'll see the "Hello, World!" printed in the terminal window (the default behavior of a new console app). Now, move to step 2 so you can modify the application and generate a Word document.
Open Program.cs in Visual Studio or Visual Studio Code and modify it:
using System;
using System.Drawing;
using GrapeCity.Documents.Word;
namespace MyApp
{
// One of the simplest ways to create a "Hello, World!" DDCX.
public class HelloWorld
{
public GcWordDocument CreateDocx()
{
// Create a new Word document:
GcWordDocument doc = new GcWordDocument();
// Add a paragraph with the 'Hello, World!' text to the first section:
doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello, World!");
// Done:
doc.Save("HelloWorld.docx");
}
}
}
On Windows or MAC, click the "start debugging" button in Visual Studio.
On Linux, enter the following in a terminal window:
$ cd ~/MyApp
$ dotnet run # runs MyApp
That's all it takes to generate a Word document using GcWord. The HelloWorld.docx should now be in your project directory. In another article, we demonstrate how to generate Word documents in code using GcWord.