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.


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.

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.

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.

Tags: , , , , , ,

This is a unique website which will require a more modern browser to work! Please upgrade today!