# DocX – Add image to Microsoft Word document programmatically

Following code is an example on how to add picture to Microsoft Word document using C# without needing to have Microsoft…

blog

05 Mar 2016 de

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

[Bilder ansehen (3)](#pf-content-image-1-0)

Following code is an example on how to add picture to **Microsoft Word** document using **C#** without needing to have **Microsoft Word** installed.

### 📌 Prerequisites

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

- [DocX](https://docx.codeplex.com) package from **Codeplex **or straight from [GitHub sources](https://github.com/WordDocX/DocX)
- ** 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 **how to insert picture with and without rotation into Microsoft Word**

```csharp
static void HelloWorldAddPictureToWord() {
    Console.WriteLine("\tHelloWorldAddPictureToWord()");

    // Create a document.
    using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
    {
        // Add an image into the document.
        RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
        rd.Up(2);
        Image image = document.AddImage(rd.Path + @"\images\logo_template.png");

        // Create a picture (A custom view of an Image).
        Picture picture = image.CreatePicture();
        picture.Rotation = 10;
        picture.SetPictureShape(BasicShapes.cube);

        // Insert a new Paragraph into the document.
        Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
        title.Alignment = Alignment.center;

        // Insert a new Paragraph into the document.
        Paragraph p1 = document.InsertParagraph();

        // Append content to the Paragraph
        p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
        p1.AppendLine();
        p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
        p1.AppendLine();

        // Insert a new Paragraph into the document.
        Paragraph p2 = document.InsertParagraph();
        // Append content to the Paragraph.

        p2.AppendLine("Is it correct?");
        p2.AppendLine();

        // Lets add another picture (without the fancy stuff)
        Picture pictureNormal = image.CreatePicture();

        Paragraph p3 = document.InsertParagraph();
        p3.AppendLine("Lets add another picture (without the fancy  rotation stuff)");
        p3.AppendLine();
        p3.AppendPicture(pictureNormal);

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

### 📄 Example Output

After running above code following, selected file should be created **HelloWorldAddPictureToWord.docx**

[!\[Generated HelloWorldAddPictureToWord.docx file created by the DocX example\](/wp-content/uploads/2016/03/img_56dab96c9d322.png)](/wp-content/uploads/2016/03/img_56dab96c9d322.png)

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

[!\[Microsoft Word document showing the inserted image produced by the DocX sample\](/wp-content/uploads/2016/03/img_56dab931918c1.png)](/wp-content/uploads/2016/03/img_56dab931918c1.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, as well an image. Both are **required** for the code sample to work and are included in the sources of DocX.

[!\[Example source assets folder containing the image file required by the DocX sample\](/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 use your own image in it's place. 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;
        }

    }

}
```

## Zugehörige Projekte

Projekte, die zu Tags, Kategorien und Inhalt dieses Beitrags passen.

[Alle Projekte](/de/projekte/)

Projekte werden abgeglichen...

[Zurück zum Blog](/blog)

## Ähnliche Beiträge

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

<!-- PowerForge markdown artifact sha256:0E9C842E78002F35C73E2DDD6BC1A0864B23489E74BA09E5652C9F1DAF4B4E03 -->
