Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Get Started

    • 9 minutes to read

    Important

    The Word Processing Document API library is currently available as a Community Technology Preview (CTP). Do not use this pre-release library in mission-critical applications. Try the library and share your feedback to help us make final adjustments before the official release. To start a conversation, submit a ticket through the DevExpress Support Center.

    Important

    You need a license for the DevExpress Office & PDF File API Subscription or DevExpress Universal Subscription to use this library in production code.

    Follow this tutorial to create a console application that uses the Word Processing Document API to generate a sales report. The application adds a title, a paragraph with formatted text, and a table to a document, saves the document to a DOCX file, and exports the document to PDF.

    DevExpress Word Processing Document API - Get Started

    Create a .NET Application

    1. Start Visual Studio and create a new Console App project.

    2. Install the DevExpress.Docs.Word NuGet package:

      dotnet add package DevExpress.Docs.Word
      

    Refer to the following help topic for additional information: Install Packages from NuGet.org: Visual Studio, VS Code, and JetBrains Rider.

    Create a .NET Framework Application

    1. Start Visual Studio and create a new Console App (.NET Framework) project that targets .NET Framework 4.6.2 or later.

    2. Install the DevExpress.Docs.Word NuGet package. If you used the DevExpress Unified Component Installer to install DevExpress products, you can add references to the following assemblies instead:

      • DevExpress.Charts.v26.2.Core.dll
      • DevExpress.Data.v26.2.dll
      • DevExpress.DataAccess.v26.2.dll
      • DevExpress.DataVisualization.v26.2.Core.dll
      • DevExpress.Docs.Core.v26.2.dll
      • DevExpress.Docs.Word.v26.2.dll
      • DevExpress.Drawing.v26.2.dll
      • DevExpress.Office.v26.2.Core.dll
      • DevExpress.Pdf.v26.2.Core.dll
      • DevExpress.Pdf.v26.2.Drawing.dll
      • DevExpress.Printing.v26.2.Core.dll
      • DevExpress.RichEdit.v26.2.Core.dll
      • DevExpress.Spreadsheet.v26.2.Core.dll
      • DevExpress.XtraCharts.v26.2.dll
      • DevExpress.XtraTreeMap.v26.2.dll

    Create a Document

    The WordDocument class represents a Word document. A new document contains a single section and no paragraphs. Use the WordDocument.Body property to access the main document content.

    Replace the contents of the Program.cs (Program.vb) file with the following code:

    using System.Drawing;
    using System.IO;
    using DevExpress.Docs;
    using DevExpress.Docs.Word;
    
    // Create a document.
    using var document = new WordDocument();
    
    // Paste the code from the following sections here.
    

    Add a Title

    A new document contains the Normal paragraph style only. To create a paragraph style, call the ParagraphStyleCollection.Add(name) method of the WordDocument.ParagraphStyles collection. Use the ParagraphStyle.CharacterProperties and ParagraphStyle.ParagraphProperties properties to specify style formatting settings. The font size is in points. Paragraph spacing is in document units (1/300 of an inch).

    Call the DocumentFragment.AddParagraph(text) method to append a paragraph to the document body. To apply a style to a paragraph, set the Paragraph.StyleName property to the style name.

    The following code snippet creates the Report Title paragraph style and applies the style to the title paragraph:

    // Create a paragraph style for the report title.
    var titleStyle = document.ParagraphStyles.Add("Report Title");
    titleStyle.ParentName = "Normal";
    titleStyle.CharacterProperties.FontSize = 20;
    titleStyle.CharacterProperties.Bold = true;
    titleStyle.CharacterProperties.ForeColor = new OfficeColor(Color.SteelBlue);
    titleStyle.ParagraphProperties.SpacingAfter = 60;
    
    // Add the title and apply the style.
    var title = document.Body.AddParagraph("Quarterly Sales Report");
    title.StyleName = titleStyle.Name;
    

    Add Formatted Text

    A paragraph can contain multiple text fragments with different character formatting settings. Call the Paragraph.AddText(text) method to add a text fragment to the end of a paragraph. The AddText method returns the created TextFragment object. Use the TextFragment.CharacterProperties property to format the text fragment.

    The following code snippet adds a paragraph and applies bold text formatting to the 14% text fragment:

    var summary = document.Body.AddParagraph("Total sales grew by ");
    var growth = summary.AddText("14%");
    growth.CharacterProperties.Bold = true;
    summary.AddText(" in the second quarter.");
    

    Add a Table

    To create a table, pass the number of rows and columns to the Table(rowCount, columnCount) constructor. Call the DocumentFragment.Add(block) method to append the table to the document body.

    Use the Table.Rows and TableRow.Cells properties to access rows and cells. New table cells contain no blocks. To add text to a cell, add a Paragraph object to the TableCell.Content collection.

    The following code snippet adds a table with four rows and three columns and populates table cells with sales data:

    var data = new[,] {
        { "Region", "Q1", "Q2" },
        { "North", "$12,000", "$15,500" },
        { "South", "$9,800", "$10,200" },
        { "Total", "$21,800", "$25,700" }
    };
    var table = new Table(4, 3);
    document.Body.Add(table);
    for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) {
        var row = table.Rows[rowIndex];
        for (int columnIndex = 0; columnIndex < row.Cells.Count; columnIndex++)
            row.Cells[columnIndex].Content.Add(new Paragraph(data[rowIndex, columnIndex]));
    }
    

    Format the Table

    Use the Table.PreferredWidth property to specify table width. Call the TableWidth.Percent(value) method to specify the width as a percentage.

    To add table borders, create a TableBorder object with a line style, thickness, and color. Assign this object to TableBorders properties (for example, TableBorders.Top). The TableProperties.Borders property provides access to table borders.

    Use the TableCellProperties.BackColor property to specify cell background color.

    The following code snippet sets the table width to 100%, adds table borders, and applies a background color to header cells:

    // Specify table width and borders.
    table.PreferredWidth = TableWidth.Percent(100);
    var border = new TableBorder(BorderLineStyle.Single, 3, new OfficeColor(Color.SteelBlue));
    var borders = table.Properties.Borders;
    borders.Left = borders.Top =
    borders.Right = borders.Bottom =
    borders.InsideHorizontal = borders.InsideVertical = border;
    
    // Apply a background color to header cells.
    foreach (var cell in table.Rows[0].Cells)
        cell.Properties.BackColor = new OfficeColor(Color.AliceBlue);
    

    Save the Document and Export It to PDF

    Call the WordDocument.Save(stream) method to save the document. If you pass a FileStream to the Save method, the API determines the output format from the file name extension. Call the WordDocument.ExportToPdf(stream) method to export the document to PDF.

    The following code snippet saves the document to a DOCX file and exports the document to a PDF file:

    Directory.CreateDirectory("output");
    
    using var docxStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(docxStream);
    
    using var pdfStream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(pdfStream);
    

    Results

    Run the application. The output folder contains Document.docx and Document.pdf files.

    The following image shows the generated document:

    DevExpress Word Processing Document API - Get Started

    The following code snippet contains the complete application code:

    using System.Drawing;
    using System.IO;
    using DevExpress.Docs;
    using DevExpress.Docs.Word;
    
    // Create a document.
    using var document = new WordDocument();
    
    // Create a paragraph style for the report title.
    var titleStyle = document.ParagraphStyles.Add("Report Title");
    titleStyle.ParentName = "Normal";
    titleStyle.CharacterProperties.FontSize = 20;
    titleStyle.CharacterProperties.Bold = true;
    titleStyle.CharacterProperties.ForeColor = new OfficeColor(Color.SteelBlue);
    titleStyle.ParagraphProperties.SpacingAfter = 60;
    
    // Add the title and apply the style.
    var title = document.Body.AddParagraph("Quarterly Sales Report");
    title.StyleName = titleStyle.Name;
    
    // Add a paragraph and apply bold text formatting to a text fragment.
    var summary = document.Body.AddParagraph("Total sales grew by ");
    var growth = summary.AddText("14%");
    growth.CharacterProperties.Bold = true;
    summary.AddText(" in the second quarter.");
    
    // Add a table and populate table cells.
    var data = new[,] {
        { "Region", "Q1", "Q2" },
        { "North", "$12,000", "$15,500" },
        { "South", "$9,800", "$10,200" },
        { "Total", "$21,800", "$25,700" }
    };
    var table = new Table(4, 3);
    document.Body.Add(table);
    for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++) {
        var row = table.Rows[rowIndex];
        for (int columnIndex = 0; columnIndex < row.Cells.Count; columnIndex++)
            row.Cells[columnIndex].Content.Add(new Paragraph(data[rowIndex, columnIndex]));
    }
    
    // Specify table width and borders.
    table.PreferredWidth = TableWidth.Percent(100);
    var border = new TableBorder(BorderLineStyle.Single, 3, new OfficeColor(Color.SteelBlue));
    var borders = table.Properties.Borders;
    borders.Left = borders.Top =
    borders.Right = borders.Bottom =
    borders.InsideHorizontal = borders.InsideVertical = border;
    
    // Apply a background color to header cells.
    foreach (var cell in table.Rows[0].Cells)
        cell.Properties.BackColor = new OfficeColor(Color.AliceBlue);
    
    // Save the document and export it to PDF.
    Directory.CreateDirectory("output");
    
    using var docxStream = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(docxStream);
    
    using var pdfStream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(pdfStream);
    

    Next Steps