[]
The following quick start sections help you in getting started with the DsWord library.
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:
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.
Include the following namespace
using GrapeCity.Documents.Word;
Create a new Word document using an instance of the GcWordDocument class, through code.
// Create a new Word document:
GcWordDocument doc = new GcWordDocument();
DsWord 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!");
!type=note
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.
Save the document using Save method of the DsWordDocument class.
//Save the created Word file
doc.Save("CreateDoc.docx");
!type=note
Note that the file is saved to the default location, which is the "bin/Debug" folder of the application.
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:
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.
Include the following namespace
using GrapeCity.Documents.Word;
Create a new instance of the DsWordDocument class to load an existing document.
// Create a new Word document:
GcWordDocument doc = new GcWordDocument();
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.
doc.Load("SampleDoc.docx");
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.");
Save the document using the Save method.
//Save the modifed Word file
doc.Save("SampleDoc_Modified.docx", DocumentType.Document);