There's no doubt that Word document is one of the most popular document file types today. Because Word document is an ideal file format for generating letters, memos, reports, term papers, novels and magazines, etc. In this article, you will learn how to create a simple Word document from scratch in C# and VB.NET by using Spire.Doc for .NET.
Spire.Doc for .NET provides the Document class to represent a Word document model, allowing users to read and edit existing documents or create new ones. A Word document must contain at least one section (represented by Section class) and each section is a container for basic Word elements like paragraphs, tables, headers, footers and so on. The table below lists the important classes and methods involved in this tutorial.
Member | Description |
Document class | Represents a Word document model. |
Section class | Represents a section in a Word document. |
Paragraph class | Represents a paragraph in a section. |
ParagraphStyle class | Defines the font formatting information that can be applied to a paragraph. |
Section.AddParagraph() method | Adds a paragraph to a section. |
Paragraph.AppendText() method | Appends text to a paragraph at the end. |
Paragraph.ApplyStyle() method | Applies a style to a paragraph. |
Document.SaveToFile() method | Saves the document to a Word file with an extension of .doc or .docx. This method also supports saving the document to PDF, XPS, HTML, PLC, etc. |
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Create a Simple Word Document
The following are the steps to create a simple Word document that contains several paragraphs by using Spire.Doc for .NET.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Set the page margins through Section.PageSetUp.Margins property.
- Add several paragraphs to the section using Section.AddParagraph() method.
- Add text to the paragraphs using Paragraph.AppendText() method.
- Create a ParagraphStyle object, and apply it to a specific paragraph using Paragraph.ApplyStyle() method.
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace CreateWordDocument { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Add a section Section section = doc.AddSection(); //Set the page margins section.PageSetup.Margins.All = 40f; //Add a paragraph as title Paragraph titleParagraph = section.AddParagraph(); titleParagraph.AppendText("Introduction of Spire.Doc for .NET"); //Add two paragraphs as body Paragraph bodyParagraph_1 = section.AddParagraph(); bodyParagraph_1.AppendText("Spire.Doc for .NET is a professional Word.NET library specifically designed " + "for developers to create, read, write, convert, compare and print Word documents on any.NET platform " + "(.NET Framework, .NET Core, .NET Standard, Xamarin & Mono Android) with fast and high-quality performance."); Paragraph bodyParagraph_2 = section.AddParagraph(); bodyParagraph_2.AppendText("As an independent Word .NET API, Spire.Doc for .NET doesn't need Microsoft Word to " + "be installed on neither the development nor target systems. However, it can incorporate Microsoft Word " + "document creation capabilities into any developers' .NET applications."); //Create a style for title paragraph ParagraphStyle style1 = new ParagraphStyle(doc); style1.Name = "titleStyle"; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.Purple; style1.CharacterFormat.FontName = "Times New Roman"; style1.CharacterFormat.FontSize = 12; doc.Styles.Add(style1); titleParagraph.ApplyStyle("titleStyle"); //Create a style for body paragraphs ParagraphStyle style2 = new ParagraphStyle(doc); style2.Name = "paraStyle"; style2.CharacterFormat.FontName = "Times New Roman"; style2.CharacterFormat.FontSize = 12; doc.Styles.Add(style2); bodyParagraph_1.ApplyStyle("paraStyle"); bodyParagraph_2.ApplyStyle("paraStyle"); //Set the horizontal alignment of paragraphs titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center; bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Justify; bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Justify; //Set the first line indent bodyParagraph_1.Format.FirstLineIndent = 30; bodyParagraph_2.Format.FirstLineIndent = 30; //Set the after spacing titleParagraph.Format.AfterSpacing = 10; bodyParagraph_1.Format.AfterSpacing = 10; //Save to file doc.SaveToFile("WordDocument.docx", FileFormat.Docx2013); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.