DEV Community

jelizaveta
jelizaveta

Posted on

A Simple Tutorial for Creating Word Documents Using C# (Quick Start)

In modern software development, automated document generation has become increasingly important. With libraries like Spire.Doc for .NET, creating and manipulating Word documents in C# is straightforward. This article will explain how to use Spire.Doc to create a simple Word document that includes titles, paragraphs, and other text elements.

Introduction to Spire.Doc for .NET

Spire.Doc is a powerful .NET document processing component that allows developers to create, read, edit, and save Word documents in C# and VB.NET. This library supports various formats, including DOC, DOCX, HTML, and PDF. Users can easily control the content and style of documents through code, generating outputs that meet their needs.

NuGet Installation

To use Spire.Doc in your project, you can easily install it via the NuGet package manager. Simply enter the following command in the command line:

Install-Package Spire.Doc
Enter fullscreen mode Exit fullscreen mode

Once the installation is complete, you can start creating Word documents using Spire.Doc.

Sample Code

The following code sample demonstrates how to create a simple Word document containing titles and paragraphs using C# and Spire.Doc.

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace CreateSimpleWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();

            // Add a section
            Section section = document.AddSection();

            // Set page margins
            section.PageSetup.Margins.All = 60f;

            // Add a title paragraph
            Paragraph title_para = section.AddParagraph();
            TextRange textRange = title_para.AppendText("This is the Title");
            title_para.ApplyStyle(BuiltinStyle.Title);
            textRange.CharacterFormat.FontName = "SimSun";

            // Add several sub-title paragraphs
            string[] headings = { "This is Heading 1", "This is Heading 2", "This is Heading 3", "This is Heading 4" };
            for (int i = 0; i < headings.Length; i++)
            {
                Paragraph heading = section.AddParagraph();
                textRange = heading.AppendText(headings[i]);
                heading.ApplyStyle((BuiltinStyle)((int)BuiltinStyle.Heading1 + i));
                textRange.CharacterFormat.FontName = "SimSun";
            }

            // Add a paragraph
            Paragraph normal_para = section.AddParagraph();
            normal_para.AppendText("This is a paragraph.");

            // Create paragraph style
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "paraStyle";
            style.CharacterFormat.FontName = "SimSun";
            style.CharacterFormat.FontSize = 13f;
            style.CharacterFormat.TextColor = Color.Brown;
            document.Styles.Add(style);

            // Apply the custom style to the specified paragraph
            normal_para.ApplyStyle("paraStyle");

            // Save the document
            document.SaveToFile("AddText.docx", FileFormat.Docx);

            // Release resources
            document.Dispose();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation

  1. Creating a Document Object : First, we instantiate a Document object, which is central to the document.
  2. Adding a Section : Using the AddSection() method allows us to add new sections to the document.
  3. Setting Page Margins : The PageSetup.Margins property allows for easy margin setting.
  4. Adding Titles and Paragraphs :
    • We can add paragraphs using the AddParagraph() method and utilize AppendText() to add text.
    • Spire.Doc allows for the use of built-in styles, applying different styles to paragraphs via the ApplyStyle() method.
  5. Customizing Paragraph Styles : Using the ParagraphStyle class, we can define our own paragraph styles and apply them to paragraphs.
  6. Saving the Document : Finally, we use the SaveToFile() method to save the document in .docx format.

More Features

If you would like to learn how to add images, lists, and other more complex elements to a Word document, you can refer to Spire.Doc’s online tutorials. These tutorials cover the library's more advanced features, helping you better master document generation techniques.

Conclusion

With this introduction, you should be able to create a Word document containing basic elements using C# and Spire.Doc. Whether you're generating reports, contracts, or any other documents, Spire.Doc offers a wealth of features to meet various needs. Continue exploring more features, and you'll be able to create more complex and professional documents.

Top comments (0)