Skip to main content

Get Started - Create a Word Document

  • 3 minutes to read

This tutorial describes how to get started with the Word Processing Document API for .NET Framework and .NET Core.

Create a .NET Framework Application

  1. Start Visual Studio and create a new Console Application (.NET Framework) project.

  2. Install the DevExpress.RichEdit.Core NuGet package or add references to the following libraries (if you have the DevExpress Unified Component Installer installed on your machine):

    • DevExpress.Data.v21.2.dll
    • DevExpress.Office.v21.2.Core.dll
    • DevExpress.RichEdit.v21.2.Core.dll
    • DevExpress.Printing.v21.2.Core.dll
    • DevExpress.Pdf.v21.2.Core.dll

    To follow this example, you should also add a reference to System.Drawing.dll.

  3. Paste the code from below in the Main method of the Program.cs file (or the Main procedure of the Module1.vb file for Visual Basic). This example creates and saves the Test.docx file, and opens it in the default application registered for this file type.

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Drawing;
    using System.Diagnostics;
    
    static void Main(string[] args)
    {
      string fileName = "Test.docx";
      using (var wordProcessor = new RichEditDocumentServer())
      {
         Document doc = wordProcessor.Document;
         doc.AppendText("This document is generated by Word Processing Document API");
         CharacterProperties cp =
             doc.BeginUpdateCharacters(doc.Paragraphs[0].Range);
         cp.ForeColor = Color.FromArgb(0x83, 0x92, 0x96);
         cp.Italic = true;
         doc.EndUpdateCharacters(cp);
    
         ParagraphProperties pp =
             doc.BeginUpdateParagraphs(doc.Paragraphs[0].Range);
         pp.Alignment = ParagraphAlignment.Right;
         doc.EndUpdateParagraphs(pp);
         wordProcessor.SaveDocument(fileName, DocumentFormat.OpenXml);
      }
      Process.Start(fileName);
    }
    
  4. Run the project. The generated file appears as follows (the document is opened in Microsoft® Word®).

    RichEditDocServer_GettingStarted_Result

Create a .NET Core Application

  1. Start Visual Studio and create a new Console Application (.NET Core) project.

  2. Install the DevExpress.Document.Processor or DevExpress.RichEdit.Core NuGet package.

    Important

    You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use the DevExpress.Document.Processor package in production code.

  3. Paste the code listed below in the Main method of the Program.cs file (or the Main procedure of the Module1.vb file for Visual Basic). This example creates and saves the Test.docx file, and opens it in the default application registered for this file type.

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Drawing;
    using System.Diagnostics;
    
    static void Main(string[] args)
    {
        string fileName = "Test.docx";
        using (var wordProcessor = new RichEditDocumentServer())
        {
            Document doc = wordProcessor.Document;
            doc.AppendText("This document is generated by Word Processing Document API");
            CharacterProperties cp =
                 doc.BeginUpdateCharacters(doc.Paragraphs[0].Range);
            cp.ForeColor = Color.FromArgb(0x83, 0x92, 0x96);
            cp.Italic = true;
            doc.EndUpdateCharacters(cp);
    
            ParagraphProperties pp =
                 doc.BeginUpdateParagraphs(doc.Paragraphs[0].Range);
            pp.Alignment = ParagraphAlignment.Right;
            doc.EndUpdateParagraphs(pp);
            wordProcessor.SaveDocument(fileName, DocumentFormat.OpenXml);
        }
        Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true });
    }
    
  4. Run the project. The generated file appears as follows (the document is opened in Microsoft® Word®).

    RichEditDocServer_GettingStarted_Result