Categories: DocX

DocX – A short tutorial for beginners HelloWorld()

Beginning your coding story with DocX is very easy and intuitive even for beginners. Below you can find a quick overview on how to create a simple HelloWorld document with use of DocX. In a series of screenshots, followed by code example you can easily see how easy it is to create a simple .DOCX document.

Tutorial

Since this is the first DocX article we will try to show you how to create new project with DocX in mind from scratch.

Open up Visual Studio 2015 (or 2010/2013)

Create New Project…

Choose Console Application (if you're more advanced you can choose anything you like of course but for the sake of this tutorial we will work with Console Application. 

Choose a proper name for your application such as DocX Test Project

You should see something similar to the view as below with Visual Studio presenting you nice overview of your project in Solution Explorer, along with Program.cs file being opened up for editing. This is your main file we will work with in this tutorial.

Before we can start coding we need to add 2 references that will be required for out little program to work. One will be DocX library itself and one will be System.Drawing library. You can add new reference by simply right clicking References in Solution Explorer and choosing Add Reference…

New Reference Manager window will show up where we simply have to brows for DocX.dll which you can simply download from the projects page.

After adding the DocX.dll to the project you should have it under References as on below screen.

Now in the very same Reference Manager but in Assemblies section we need to find System.Drawing and add it to references as well.

After adding it, you should have both DocX and System.Drawing references in your project.

While DocX is playing the main part here we will be using some Fonts and Colors in our document hence we need the System.Drawing library to be able to use their secrets.

We just now go ahead and simply add couple of code lines to create a simple DocX document. We've added 2 References to our Project but we also need to make sure our Namespace can use this references. This is done by adding using System.Drawing and using Novacode.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Novacode;

namespace DocX_Test_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloWorld();
        }
        static void HelloWorld()
        {
            Console.WriteLine("tHelloWorld()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docsHello World.docx"))
            {
                // Insert a Paragraph into this document.
                Paragraph p = document.InsertParagraph();

                // Append some text and add formatting.
                p.Append("Hello World!^011Hello World!")
                .Font(new FontFamily("Times New Roman"))
                .FontSize(32)
                .Color(Color.Blue)
                .Bold();

                // Save this document to disk.
                document.Save();
                Console.WriteLine("tCreated: docs\Hello World.docxn");
            }
        }
    }
}

As you can see besides usings we have added new method called HelloWorld() and added HelloWorld(); inside Main() method to execute it. While at this point you could simply run the program you should also notice line 22 where the path for new file is defined. If you've not created docs folder before in your projects folder you would get an error. So while you can always do it manually you can also simply add a new method called Setup() and execute it before running HelloWorld() method. This method creates new docs directory if it doesn't exists yet.

Unfortunately due to use of another library we need to add additional using on top as well. Our code after little changes will look like this.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Novacode;

namespace DocX_Test_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Setup();
            HelloWorld();
        }
        static void HelloWorld()
        {
            Console.WriteLine("tHelloWorld()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docsHello World.docx"))
            {
                // Insert a Paragraph into this document.
                Paragraph p = document.InsertParagraph();

                // Append some text and add formatting.
                p.Append("Hello World!^011Hello World!")
                .Font(new FontFamily("Times New Roman"))
                .FontSize(32)
                .Color(Color.Blue)
                .Bold();

                // Save this document to disk.
                document.Save();
                Console.WriteLine("tCreated: docs\Hello World.docxn");
            }
        }
        private static void Setup()
        {
            if (!Directory.Exists("docs"))
            {
                Directory.CreateDirectory("docs");
            }
        }
    }
}

After making sure everything is done correctly we can now proceed to try and run the code. We can either press F5 or choose this from the Debug Menu option Start Debugging

Program should show a quick console window and if everything is ok simply execute its commands and simply disappear. As the code is very small it should be done within a second or two so you may not even notice it. After that we just need to go to our projects folder into newly created .exe binary directory and find our HelloWorld.docx document waiting there for us.

And this is how it looks like

Maybe it doesn't have much information on it but this should give you a fair idea on how easy it's to code with DocX.

This post was last modified on 29 grudnia, 2018 22:00

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

3 tygodnie ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 miesięcy ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 miesięcy ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 miesięcy ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 miesięcy ago