[]
        
(Showing Draft Content)

Quick Start

The following quick start sections help you in getting started with the GcWord library.

Create and Save a Word Document

This quick start covers how to create a simple Word document having a single page, add text to it and save it in a .NET Core or .NET Standard application. Follow the steps below to get started:

  1. Create a new GcWord document

  2. Add text to the document

  3. Save the document

A new Word document

Step 1: Create a new GcWord document

  1. Create a new application (.NET Core Console App\Windows Forms App) and install the GrapeCity.Documents.Word package to add the required dlls to the project. For detailed information on how to add the NuGet packages, see Getting Started.

  2. Include the following namespace

    • using GrapeCity.Documents.Word;

  3. Create a new Word document using an instance of the GcWordDocument class, through code.

    // Create a new Word document:
    GcWordDocument doc = new GcWordDocument();

Step 2: Add text to the document

GcWord provides two methods to add text to the document:

  • Access the first section from the body of the document using the GetRange method and add a paragraph to the paragraph collection using Add method of the ParagraphCollection class.

  • Or, Add a paragraph to the paragraph collection using the AddParagraph method of Body class.

// Add a paragraph with the 'Hello, World!' text to the first section:
doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello World!");

// Or
doc.Body.AddParagraph("Hello World!");

Note: GcWord provides various content elements helper methods in the classes to add different content elements directly, thus making the code shorter, clearer, and the content elements easily accessible. For more information, see Helper Methods for Adding Content.

Step 3: Save the document

Save the document using Save method of the GcWordDocument class.

//Save the created Word file
doc.Save("CreateDoc.docx");

Note that the file is saved to the default location, which is the "bin/Debug" folder of the application.

Load and Modify a Word Document

This quick start covers how to load an existing Word document, modify it and save it using a .NET Core or .NET Standard application. Follow the steps below to get started:

  1. Load an existing document in GcWord

  2. Modify the document

  3. Save the document

Modified Word document

Step 1: Load an existing document in GcWord

  1. Create a new application (.NET Core Console App\Windows Forms App) and install the GrapeCity.Documents.Word package to add the required dlls to the project. For detailed information on how to add the NuGet packages, see Getting Started.

  2. Include the following namespace

    • using GrapeCity.Documents.Word;

  3. Create a new instance of the GcWordDocument class to load an existing document.

    // Create a new Word document:
    GcWordDocument doc = new GcWordDocument();
  4. Load an existing document using Load method of the GcWordDocument class. In this example, the document to be loaded is placed in the "bin/Debug" folder of the application. In case you want to load a document from some other location in your system, you can update the location accordingly in the code.

    doc.Load("SampleDoc.docx");

Step 2: Modify the document

To modify the document, access the first section from body of the document using the GetRange method and add a paragraph to the paragraph collection using Add method of the ParagraphCollection class.

// Add a new paragraph with the specific content, to the existing document
Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.Add("This document" +
    "has been modified. A new paragraph is added to the document.");

Step 3: Save the document

Save the document using the Save method.

//Save the modifed Word file
doc.Save("SampleDoc_Modified.docx", DocumentType.Document);