# 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…

blog

03 Oct 2015 pl

[Zobacz obrazy (12)](#pf-content-image-1-0)

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](https://docx.codeplex.com). 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…**

[!\[DocX-VisualStudio-HelloWorld-Example1\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example1.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example1.png)

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. **

[!\[DocX-VisualStudio-HelloWorld-Example2\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example2.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example2.png)

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

[!\[DocX-VisualStudio-HelloWorld-Example3\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example3.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example3.png)

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.

[!\[DocX-VisualStudio-HelloWorld-Example4\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example4.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example4.png)

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…**

[!\[DocX-VisualStudio-HelloWorld-Example6\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example6.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example6.png)

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](https://docx.codeplex.com/releases/view/616950)**.

[!\[DocX-VisualStudio-HelloWorld-Example7\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example7.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example7.png)

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

[!\[DocX-VisualStudio-HelloWorld-Example8\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example8.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example8.png)

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

[!\[DocX-VisualStudio-HelloWorld-Example9.1\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example9.1.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example9.1.png)

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

[!\[DocX-VisualStudio-HelloWorld-Example9.2\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example9.2.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example9.2.png)

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**.

```csharp
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.

```csharp
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**

[!\[DocX-VisualStudio-HelloWorld-Example12\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example12.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example12.png)

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.

[!\[DocX-VisualStudio-HelloWorld-Example13\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example13.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example13.png)

And this is **how it looks** like

[!\[DocX-VisualStudio-HelloWorld-Example14\](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example14.png)](/wp-content/uploads/2015/10/DocX-VisualStudio-HelloWorld-Example14.png)

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**.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:954C2ADB0D948723AEEF18A2D52A3C3ED60ED6F164FEE0F428971B30070116B7 -->
