Write Word file in C# and VB.NET

Creating document content programmatically with GemBox.Document is easy and straightforward. This is done by composing document elements through constructors.

GemBox.Document supports writing Word documents in C# and VB.NET to a file or a stream. The document file format is specified through SaveOptions derived classes or a file name extension.

The following sample shows how to programmatically create a new document, how to use non-Latin Unicode characters, and how to write a Word document to a file.

Screenshot

Writing Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.Linq;
3using System.Text;
4using GemBox.Document;
5using GemBox.Document.Tables;
6using System.Text.RegularExpressions;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15
16        DocumentModel document = new DocumentModel();
17
18        document.Sections.Add(
19            new Section(document,
20                new Paragraph(document,
21                    new Run(document, "English: Hello"),
22                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
23                    new Run(document, "Russian: "),
24                    new Run(document, new string(new char[] { '\u0417', '\u0434', '\u0440', '\u0430', '\u0432', '\u0441', '\u0442', '\u0432', '\u0443', '\u0439', '\u0442', '\u0435' })),
25                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
26                    new Run(document, "Chinese: "),
27                    new Run(document, new string(new char[] { '\u4f60', '\u597d' }))),
28                new Paragraph(document, "In order to see Russian and Chinese characters you need to have appropriate fonts on your machine.")));
29
30        document.Save("Writing.docx");
31    }
32}
1Imports System
2Imports System.Linq
3Imports System.Text
4Imports GemBox.Document
5Imports GemBox.Document.Tables
6Imports System.Text.RegularExpressions
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim document As DocumentModel = New DocumentModel
16
17        document.Sections.Add(
18            New Section(document,
19                New Paragraph(document,
20                    New Run(document, "English: Hello"),
21                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
22                    New Run(document, "Russian: "),
23                    New Run(document, New String(New Char() {ChrW(&H417), ChrW(&H434), ChrW(&H440), ChrW(&H430), ChrW(&H432), ChrW(&H441), ChrW(&H442), ChrW(&H432), ChrW(&H443), ChrW(&H439), ChrW(&H442), ChrW(&H435)})),
24                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
25                    New Run(document, "Chinese: "),
26                    New Run(document, New String(New Char() {ChrW(&H4F60), ChrW(&H597D)}))),
27                New Paragraph(document, "In order to see Russian and Chinese characters you need to have appropriate fonts on your machine.")))
28
29        document.Save("Writing.docx")
30
31    End Sub
32
33End Module

Check next sample or find out more about GemBox.Document and GemBox Software.