# DocX – Add horizontal lines to Microsoft Word document programmatically

Following code is an example on how to add horizontal lines to Microsoft Word document by using DocX package programmatically…

blog

26 May 2016 en

![DocX – Add horizontal lines to Microsoft Word document programmatically](/wp-content/uploads/2016/03/DocX-MicrosoftWord.png)

[View images (3)](#pf-content-image-1-0)

Following code is an **example** on how to add horizontal lines to **Microsoft Word** document by using **DocX package** programmatically in C# without needing of Microsoft Word installed.

### 📌 Prerequisites

Following **prerequisites** are required to create **Microsoft Word** document programmatically:

- [DocX](https://docx.codeplex.com) package
- ** Visual Studio 2013** / **Visual Studio 2015** (Free Community Edition will do)
- Basic understanding on how to code in **C# (C Sharp)**
- Basic tutorial on how to start coding with **DocX** **package** can be found on our blog post [DocX – A short tutorial for beginners HelloWorld()](https://evotec.xyz/docx-a-short-tutorial-for-beginners-helloworld/)

### 💻 Example code

Following code provides basic example usage on adding horitzonal lines to your Word Documents programatically with C#.

```csharp
static void HelloWorldInsertHorizontalLine()
{
    Console.WriteLine("\tHelloWorldInsertHorizontalLine()");

    // Create a new document.
    using (DocX document = DocX.Create(@"docs\HelloWorldInsertHorizontalLine.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();
        p.InsertHorizontalLine("double", 6, 1, "auto");

        Paragraph p1 = document.InsertParagraph();
        p1.InsertHorizontalLine("double", 6, 1, "red");
        Paragraph p2 = document.InsertParagraph();
        p2.InsertHorizontalLine("single", 6, 1, "red");
        Paragraph p3 = document.InsertParagraph();
        p3.InsertHorizontalLine("triple", 6, 1, "blue");
        Paragraph p4 = document.InsertParagraph();
        p4.InsertHorizontalLine("double", 3, 10, "red");


        // Save this document to disk.
        document.Save();
        Console.WriteLine("\tCreated: docs\\HelloWorldInsertHorizontalLine.docx\n");
    }
}
```

### 📄 Example Output

After running above code new file is created called HelloWorldInsertHorizontalLine.docx

[!\[Generated HelloWorldInsertHorizontalLine.docx file\](/wp-content/uploads/2016/05/img_5746efc9a022c.png)](/wp-content/uploads/2016/05/img_5746efc9a022c.png)

After opening **Microsoft Word** .**docx** document it should look like this:

[!\[Microsoft Word document with inserted horizontal lines\](/wp-content/uploads/2016/05/img_5746effab9d29.png)](/wp-content/uploads/2016/05/img_5746effab9d29.png)

### 🔹 Example Notes

Above example is part of **DocX Examples** and can be found in DocX [source package](https://github.com/WordDocX/DocX) under **Examples \ Program.cs. **This example uses **RelativeDirectory.cs** class. It's **required** for the code sample to work and are included in the sources of DocX.

[!\[DocX example source showing RelativeDirectory.cs reference\](/wp-content/uploads/2016/03/img_56dabd92ba257.png)](/wp-content/uploads/2016/03/img_56dabd92ba257.png)

Keep that in mind when working with the code to either download the DocX package or change the code appropriately. Below you can find **Relative Directory** class just in case you have no access to sources.

```csharp
using System;
using System.IO;

namespace Examples
{
    class RelativeDirectory
    {
        // Author D. Bolton see https://cplus.about.com (c) 2010
        private DirectoryInfo _dirInfo;

        public string Dir
        {
            get
            {
                return _dirInfo.Name;
            }
        }

        public string Path
        {
            get { return _dirInfo.FullName; }
            set
            {
                try
                {
                    DirectoryInfo newDir = new DirectoryInfo(value);
                    _dirInfo = newDir;
                }
                catch
                {
                    // silent
                }
            }
        }
        public RelativeDirectory()
        {
            _dirInfo = new DirectoryInfo(Environment.CurrentDirectory);
        }

        public RelativeDirectory(string absoluteDir)
        {
            _dirInfo = new DirectoryInfo(absoluteDir);
        }

        public Boolean Up(int numLevels)
        {
            for (int i = 0; i &lt; numLevels; i++)
            {
                DirectoryInfo tempDir = _dirInfo.Parent;
                if (tempDir != null)
                    _dirInfo = tempDir;
                else
                    return false;
            }
            return true;
        }

        public Boolean Up()
        {
            return Up(1);
        }

        public Boolean Down(string match)
        {
            DirectoryInfo[] dirs = _dirInfo.GetDirectories(match + '*');
            _dirInfo = dirs[0];
            return true;
        }

    }

}
```

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [DocX – Password protect Microsoft Word document programmatically](/blog/docx-password-protect-microsoft-word-document-programmatically/)
- [DocX – Add image to Microsoft Word document programmatically](/blog/docx-add-image-to-microsoft-word-document-programmatically/)
- [DocX – Add nested lists to Microsoft Word document programmatically](/blog/docx-add-nested-lists-microsoft-word-document-programmatically/)

<!-- PowerForge markdown artifact sha256:02F34B1FA13BA0DFA037928B6C2AD1763142454E786614278228D4472251CDA2 -->
