How to Insert Images to Word Documents in C#

Add Images to Word Documents in .NET

Adding images to Word documents programmatically is a common requirement in document automation. Whether you're generating reports, creating invoices, or building dynamic documents, inserting and customizing images can enhance your document's visual appeal.

In this article, we'll explore how to insert images into Word documents using Spire.Doc for .NET, covering local files, byte arrays, and advanced image customization.

.NET Library for Adding Images to Word

Spire.Doc for .NET is a powerful library that enables developers to create, edit, and manipulate Word documents without Microsoft Office dependencies. It provides straightforward methods to insert and format images in Word files.

Key Features:

  • Insert images from local storage or byte arrays.
  • Adjust image size, rotation, and positioning.
  • Control text wrapping around images.
  • Support for various image formats (PNG, JPG, BMP, etc.).

To get started, downoad Spire.Doc from our offical website and reference the DLLs in your project. Or, you can install it via NuGet through the following command:

PM> Install-Package Spire.Doc

Insert an Imge from Local to Word

The simplest method for inserting images into a Word document is to load them directly from your file system. Using Spire.Doc's AppendPicture() method, you can easily specify the file path of the image. This method automatically detects the image format and embeds it into the document.

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

namespace InsertImageFromLocal
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();

            // Add a section
            Section section = document.AddSection();

            // Add a paragraph
            Paragraph paragraph = section.AddParagraph();

            // Append a picture from the local disk
            paragraph.AppendPicture("C:\\Users\\Administrator\\Desktop\\MyPic.png");

            // Save the document
            document.SaveToFile("output.docx", FileFormat.Docx);

            // Dispose resources
            document.Dispose();
        }
    }
}

Insert Images to Word in C#

Insert an Image from a Byte Array to Word

In dynamic applications, integrating images from web sources or databases is often essential for enhancing user experience and content relevance.This method downloads images as byte arrays and injecte into the document using a MemoryStream .

By utilizing this technique, developers can dynamically populate documents with up-to-date content, such as logos or product images, directly from online resources or databases.

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

namespace InsertImageFromByteArray
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            // URL of the image
            string imageUrl = "https://example.com/image.png";

            // Download the image
            byte[] imageBytes;
            using (HttpClient client = new HttpClient())
            {
                imageBytes = await client.GetByteArrayAsync(imageUrl);
            }

            // Create a Document object
            Document document = new Document();

            // Add a section
            Section section = document.AddSection();

            // Add a paragraph
            Paragraph paragraph = section.AddParagraph();

            // Append a picture from the byte array
            using (MemoryStream stream = new MemoryStream(imageBytes))
            {
                paragraph.AppendPicture(stream);
            }

            // Save the document
            document.SaveToFile("output.docx", FileFormat.Docx);

            // Dispose resources
            document.Dispose();
        }
    }
}

Further Customize the Image

Professional documents often demand precise image formatting. Spire.Doc offers extensive controls through the DocPicture class, enabling users to manipulate images effectively.

Key features include resizing to fit layouts, rotating for optimal orientation, and text wrapping options that allow text to flow seamlessly around images. Additionally, users can specify spacing and alignment to position images accurately relative to surrounding content.

// Append an image from disk
DocPicture picture = paragraph.AppendPicture("C:\\Users\\Administrator\\Desktop\\MyPic.png");

// Resize the image to 70%
picture.SetScale(70f);

// Rotate the image 10 degrees counterclockwise
picture.Rotation = -10;

// Specify top and bottom distance to 3 units
picture.DistanceTop = picture.DistanceBottom = 3;

// Set the text wrapping style around the image
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;

// Center align the paragraph containing the image
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

Conclusion

Using Spire.Doc for .NET simplifies the process of inserting and customizing images in Word documents. Whether pulling images from local storage or online sources, the library provides flexible options to enhance document presentation.

Get a Free License

To fully experience the capabilities of Spire.Doc for .NET without any evaluation limitations, you can request a free 30-day trial license.