Skip to main content Skip to footer

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

  1. Open Visual Studio for Windows.
  2. Create a new .NET Core console application.
  3. Right-click the project in the Solution Explorer and choose Manage NuGet Packages.
  4. In the package source on the top right, select nuget.org.
  5. Click the Browse tab on the top left and enter GrapeCity.Documents as the search string. You should see several GrapeCity.Documents packages listed.
  6. 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

  1. Create any application (.NET Core, ASP.NET Core, .NET Framework - any target that supports .NET Standard 2.0).
  2. Right-click the project in the Solution Explorer and choose Manage NuGet Packages.
  3. In the package source on the top left, choose nuget.org.
  4. Click the Browse tab on the top right and search for GrapeCity.Documents.
  5. On the left panel, choose GrapeCity.Documents.Word.
  6. On the right panel, click Install.
  7. 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

  1. 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
  1. Open Visual Studio Code.
  2. If you haven't already done so, from extensions, install Nuget Package Manager and activate it.
  3. 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.
  4. In the search box that opens, type GrapeCity and press Enter. This brings up the list of available GrapeCity packages.
  5. 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.

Shilpa Sharma - Product Manager

Shilpa Sharma

Product Manager
comments powered by Disqus