Getting started with Documents for Word on Windows, Mac, and Linux
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.
Step 1: Create an empty console application
Create an app using Visual Studio on Windows
- Open Visual Studio for Windows.
- Create a new .NET Core console application.
- Right-click the project in the Solution Explorer and choose Manage NuGet Packages.
- In the package source on the top right, select nuget.org.
- Click the Browse tab on the top left and enter GrapeCity.Documents as the search string. You should see several GrapeCity.Documents packages listed.
- Select GrapeCity.Documents.Word and click install. Accept the license agreement.
This adds the required references to your application. Jump to step 2 to add code in the application and generate a Word document.
Create an app using Visual Studio on MAC
- Create any application (.NET Core, ASP.NET Core, .NET Framework - any target that supports .NET Standard 2.0).
- Right-click the project in the Solution Explorer and choose Manage NuGet Packages.
- In the package source on the top left, choose nuget.org.
- Click the Browse tab on the top right and search for GrapeCity.Documents.
- On the left panel, choose GrapeCity.Documents.Word.
- On the right panel, click Install.
- Choose "I Accept" in the next screen.
This adds the required references to your application. To generate a Word document, jump to step 2.
Create an app using Visual Studio Code on Linux
- In a terminal window (you may use the terminal in Visual Studio Code), type the following commands:
$ 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
- Open Visual Studio Code.
- If you haven't already done so, from extensions, install Nuget Package Manager and activate it.
- In Visual Studio Code, press Ctrl+P to open the file command box, type > in it, find Nuget Package Manager: Add Package in the list that opens, and click it.
- In the search box that opens, type GrapeCity and press Enter. This brings up the list of available GrapeCity packages.
- Select GrapeCity.Documents.Word.
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.
Step 2: Add code to your application
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");
}
}
}
Step 3: Run the application
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.