Create and format Word document with Document SDK

The samples in C# and VB.NET show how to create and format Word document (.docx) with Document SDK. Using the SDK you can generate Word documents and adjuct paragraph and text fromatting.

C#

using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Document;
 
namespace CreateAndFormatDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Document doc = new Document(DocumentFormat.OpenXml))
            {
                Paragraph p = doc.AddParagraph();
                p.Properties.Alignment = ParagraphAlignment.Center;
                p.Text = "A heading";
 
                p = doc.AddParagraph();
                p.Properties.Alignment = ParagraphAlignment.Right;
                p.Text = "A foreword";
                p.GetTextRun(0).Properties.Italic = true;
 
                // add new paragraph with mixed text formatting
                p = doc.AddParagraph();
                p.Text = "Next paragraph is on the ";
 
                // add text run (text chunk) that should have another formatting
                TextRun tr = p.AddTextRun();
                tr.Text = "next page.";
                tr.Properties.Bold = true;
                tr.Properties.Underline = Underline.Dotted;
                tr.Properties.HighlightColor = HighlightColor.Green;
 
                // add new paragagraph with page break before its contents
                p = doc.AddParagraph();
                p.Properties.PageBreakBefore = true;
                p.Properties.Alignment = ParagraphAlignment.Justify;
                p.Text = "This paragraph is justified\nand may contain\nnasty gaps between\nwords";
 
                doc.Save("output.docx");
                System.Diagnostics.Process.Start("output.docx");
            }
        }
    }
}

 

VB.NET

Imports Bytescout.Document
 
Module Module1
 
    Sub Main()
        Using doc As New Document(DocumentFormat.OpenXml)
            Dim p As Paragraph = doc.AddParagraph()
            p.Properties.Alignment = ParagraphAlignment.Center
            p.Text = "A heading"
 
            p = doc.AddParagraph()
            p.Properties.Alignment = ParagraphAlignment.Right
            p.Text = "A foreword"
            p.GetTextRun(0).Properties.Italic = True
 
            ' add new paragraph with mixed text formatting
            p = doc.AddParagraph()
            p.Text = "Next paragraph is on the "
 
            ' add text run (text chunk) that should have another formatting
            Dim tr As TextRun = p.AddTextRun()
            tr.Text = "next page."
            tr.Properties.Bold = True
            tr.Properties.Underline = Underline.Dotted
            tr.Properties.HighlightColor = HighlightColor.Green
 
            ' add new paragagraph with page break before its contents
            p = doc.AddParagraph()
            p.Properties.PageBreakBefore = True
            p.Properties.Alignment = ParagraphAlignment.Justify
            p.Text = "This paragraph is justified" & vbLf & "and may contain" & vbLf & "nasty gaps between" & vbLf & "words"
 
            doc.Save("output.docx")
            System.Diagnostics.Process.Start("output.docx")
        End Using
 
    End Sub
 
End Module
Filed in: Document SDK