DEV Community

jelizaveta
jelizaveta

Posted on

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
Enter fullscreen mode Exit fullscreen mode

Then include the required namespace in your project:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
Enter fullscreen mode Exit fullscreen mode

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
onlineproxy profile image
OnlineProxy

Create a Document, add a Section/Paragraph, then call AppendPicture on a System.Drawing.Image you loaded from file, stream, byte, Base64, or HTTP-boom. For layout control, use HorizontalAlignment, TextWrappingStyle and absolute positioning via HorizontalOrigin/VerticalOrigin plus HorizontalPosition/VerticalPosition. Performance gotchas: dispose images/streams, save once at the end, avoid upscaling, and pre-process images.