Insert Images to Word Files Using Spire.Doc for .NET

 Images make Word documents more engaging and visually informative. Whether you’re creating a report, a brochure, or a technical manual, inserting pictures programmatically can help automate and customize your document generation process.

In this article, you’ll learn how to insert images into Word files using Spire.Doc for .NET . We’ll cover simple image insertion, image alignment and scaling, and advanced placement within paragraphs or headers/footers.

Prerequisites

Before you begin, install the Spire.Doc for .NET library. You can add it via NuGet Package Manager:

Install-Package Spire.Doc

Then include the required namespace in your project:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

Method 1: Insert an Image at a Specific Location

This basic example shows how to add an image directly into a Word document.

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertImageExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Word document
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            // Insert an image
            DocPicture picture = paragraph.AppendPicture(System.Drawing.Image.FromFile("logo.png"));

            // Save the document
            doc.SaveToFile("InsertImage.docx", FileFormat.Docx2013);
        }
    }
}

Explanation:

  • A new Word document is created.
  • The image “logo.png” is added to a paragraph.
  • The file is saved in .docx format.

Method 2: Resize and Align the Image

You can easily adjust the image’s size and alignment to match your layout needs.

DocPicture picture = paragraph.AppendPicture(System.Drawing.Image.FromFile("header.png"));
picture.Width = 200;
picture.Height = 80;
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

Tips:

  • Use picture.Width and picture.Height to scale the image.
  • Align it left, right, or center by modifying paragraph.Format.HorizontalAlignment.

Method 3: Insert Image into Header or Footer

If you need a logo or watermark to appear on every page, insert the image into the header or footer section.

HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
DocPicture headerImage = headerParagraph.AppendPicture(System.Drawing.Image.FromFile("header-logo.png"));
headerImage.Width = 100;
headerImage.Height = 50;
headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;

This ensures the image repeats automatically across all document pages.

Method 4: Insert Image Behind or In Front of Text

Spire.Doc allows you to control the text wrapping style , letting you position an image in front of or behind text.

DocPicture picture = paragraph.AppendPicture(System.Drawing.Image.FromFile("background.png"));
picture.TextWrappingStyle = TextWrappingStyle.Behind;
picture.Width = 500;
picture.Height = 700;

You can use TextWrappingStyle.Behind or TextWrappingStyle.InFrontOfText depending on your layout.

Method 5: Insert Image from Stream or Memory

When working with dynamically generated images or remote resources, inserting from a stream is efficient.

using (FileStream fs = new FileStream("chart.jpg", FileMode.Open, FileAccess.Read))
{
    DocPicture picture = paragraph.AppendPicture(System.Drawing.Image.FromStream(fs));
    picture.Width = 300;
    picture.Height = 200;
}

This approach is ideal for web or database-driven applications that handle images without saving them locally.

Conclusion

Using Spire.Doc for .NET , inserting images into Word files becomes flexible and efficient. You can:

  • Embed local or in-memory images.
  • Adjust image size, alignment, and position.
  • Insert images into headers, footers, or as backgrounds.

This makes document automation smoother, especially for generating branded reports, invoices, or templates with consistent visual styling.

By mastering these techniques, developers can produce polished, image-rich Word documents entirely through code.

评论

此博客中的热门博文

How to Convert Between Excel and CSV in C#:Based on Spire.XLS

Python Tutorial: Easily Rotate PDF Pages

Convert PDF to PNG Using Spire.PDF for Python