Document Solutions for Word
Getting Started / Quick Start
In This Topic
    Quick Start
    In This Topic

    The following quick start sections help you in getting started with the DsWord 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 DsWord document
    2. Add text to the document
    3. Save the document

    A new Word document

    Step 1: Create a new DsWord document

    1. Create a new application (.NET Core Console App\Windows Forms App) and install the DS.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.
      C#
      Copy Code
      // Create a new Word document:
      GcWordDocument doc = new GcWordDocument();
    Back to Top

    Step 2: Add text to the document

    DsWord provides two methods to add text to the document:

    C#
    Copy Code
    // 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: DsWord 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.

    Back to Top

    Step 3: Save the document

    Save the document using Save method of the DsWordDocument class.

    C#
    Copy Code
    //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.

    Back to Top

    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 DsWord
    2. Modify the document
    3. Save the document

    Modified Word document

    Step 1: Load an existing document in DsWord

    1. Create a new application (.NET Core Console App\Windows Forms App) and install the DS.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 DsWordDocument class to load an existing document.
      C#
      Copy Code
      // Create a new Word document:
      GcWordDocument doc = new GcWordDocument();
    4. Load an existing document using Load method of the DsWordDocument 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.
      C#
      Copy Code
      doc.Load("SampleDoc.docx");

    Back to Top

    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.

    C#
    Copy Code
    // 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.");
    Back to Top

    Step 3: Save the document

    Save the document using the Save method.

    C#
    Copy Code
    //Save the modifed Word file
    doc.Save("SampleDoc_Modified.docx", DocumentType.Document);
    Back to Top