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:
C# |
Copy Code |
---|---|
// Create a new Word document: GcWordDocument doc = new GcWordDocument(); |
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.
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.
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:
C# |
Copy Code |
---|---|
// Create a new Word document: GcWordDocument doc = new GcWordDocument(); |
C# |
Copy 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.
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."); |
Save the document using the Save method.
C# |
Copy Code |
---|---|
//Save the modifed Word file doc.Save("SampleDoc_Modified.docx", DocumentType.Document); |