How to Add Image to DOCX

This article was translated from English: Does it need improvement?
Translated
View the article in English

Images can be used to add visual context to an existing word document. To describe a sentence, a paragraph, or just a plain block of text. Adding images to a Word document (.docx) are made simple and straightforward with IronWord (A C# library for working with DOCX files). This tutorial will cover the essentials of adding images to a .docx file within a .NET environment.

Try IronWord

Commencez à utiliser IronWord dans votre projet aujourd'hui avec un essai gratuit.

Première étape :
green arrow pointer


Adding an Image

An image is referred programatically using its defined path in the local system. Firstly, instantiate the ImageContent class by providing the file path in string. Then, the image variable can be used all throughout the file in various locations or just to modify its properties, e.g. width, height. Finally, add the image to the .docx file using the AddImage() function. Once added, the document can be exported and saved locally. In the example below, we are adding the image inside the document without any parent node. The supported file formats are: .jpg, .png, .bmp, .tiff, .gif.

ConseilsAn image can be inserted as a child element within a paragraph for better document hierarchy. The paragraph can also define text wrapping and other text formatting properties.

:path=/static-assets/word/content-code-examples/how-to/add-image-insert-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.jpg");

// modifying image properties
image.Width = 200;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("inserted-image.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Add image

Adding Image via Stream

Images which are served locally or via a static URL are quite easy to directly add into a .docx file using the previous method. To add images which are behind a secure API, requiring authentication, the Stream method would be appropriate. In the example below, an HTTP client sends authorization tokens to the server, retrieves the authenticated image stream upon verification, and integrates it directly before exporting the final .docx file.

:path=/static-assets/word/content-code-examples/how-to/add-image-insert-image-via-http-stream.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

using (HttpClient client = new HttpClient())
{
    // Add authentication headers
    client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY_HERE");
    client.DefaultRequestHeaders.Add("User-Agent", "MyApp/1.0");

    // Get image from authenticated endpoint
    Stream authenticatedStream = client.GetStreamAsync("https://api.example.com/secure/image.png");
    doc.AddImage(authenticatedStream);
}

// Export docx
doc.SaveAs("added-image-via-http-stream.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Modifying image properties

IronWord provides default customization methods for an image.

Settings Description Example
Width Horizontal dimension of the image in pixels image.Width = 500;
Height Vertical dimension of the image in pixels image.Height = 300;
WrapText Text wrapping behavior around the image image.WrapText = WrapText.Square;
DistanceFromLeft Spacing measurement from left edge in pixels image.DistanceFromLeft = 10;
DistanceFromRight Spacing measurement from right edge in pixels image.DistanceFromRight = 10;
DistanceFromTop Spacing measurement from top edge in pixels image.DistanceFromTop = 15;
DistanceFromBottom Spacing measurement from bottom edge in pixels image.DistanceFromBottom = 15;
Position Spatial placement information (X and Y coordinates) image.Position = new ElementPosition(50, 100);
Scale Proportional sizing factors for X and Y axes image.Scale = new PointF(1.5f, 1.5f);
Translate Displacement coordinates for repositioning image.Translate = new PointF(20, 30);

Customizing Width & Height

Custom width and height can be implemented altering predefined aspect ratio. An example is shown below.

:path=/static-assets/word/content-code-examples/how-to/add-image-custom-size.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// initializing docx file
WordDocument doc = new IronWord.WordDocument();

// instantiating image file
IronWord.Models.ImageContent image = new IronWord.Models.ImageContent("sample-image.tiff");

// modifying the aspect ratio by introducing custom width
image.Width = 800;
image.Height = 200;

// AddImage function saving the image
doc.AddImage(image);

// Save and export the file
doc.SaveAs("custom-size-image.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Add Image Custom Size

Questions Fréquemment Posées

Comment puis-je ajouter une image à un fichier DOCX en utilisant C# ?

Vous pouvez ajouter une image à un fichier DOCX de manière programmatique en C# en utilisant IronWord. Il vous permet de manipuler facilement les documents Word, y compris l'insertion d'images.

Quels types d'images peuvent être ajoutés à un DOCX en utilisant IronWord ?

IronWord prend en charge divers formats d'image, y compris JPEG, PNG, BMP et GIF, qui peuvent être ajoutés à un fichier DOCX.

Est-il possible de positionner les images dans un document Word en utilisant IronWord ?

Oui, IronWord permet un contrôle précis du positionnement des images dans un document Word, vous permettant de spécifier les emplacements et l'alignement.

Puis-je ajouter plusieurs images à un seul document Word avec IronWord ?

Absolument. IronWord prend en charge l'ajout de plusieurs images à un seul document Word, permettant des mises en page de documents complexes.

IronWord prend-il en charge l'ajout d'images dans les en-têtes et les pieds de page ?

Oui, IronWord peut être utilisé pour ajouter des images à la fois dans les en-têtes et les pieds de page d'un document Word, améliorant ainsi le design de votre document.

Comment IronWord gère-t-il le redimensionnement et la mise à l'échelle des images ?

IronWord fournit des fonctionnalités pour mettre à l'échelle et redimensionner les images lorsqu'elles sont ajoutées à un document Word, garantissant qu'elles s'intègrent parfaitement dans la mise en page.

Puis-je ajouter des légendes aux images dans un fichier DOCX en utilisant IronWord ?

Oui, IronWord permet d'ajouter des légendes aux images, améliorant ainsi la clarté et le contexte dans vos documents Word.

Est-il possible de remplacer une image existante dans un document Word en utilisant IronWord ?

IronWord vous permet de remplacer des images existantes dans un document Word, offrant une flexibilité dans les mises à jour et éditions de documents.

Quels sont les prérequis système pour utiliser IronWord pour ajouter des images aux fichiers DOCX ?

IronWord nécessite la prise en charge du framework .NET et est compatible avec les environnements Windows, macOS et Linux pour l'ajout d'images aux fichiers DOCX.

Puis-je automatiser le processus d'ajout d'images aux fichiers DOCX en utilisant IronWord ?

Oui, IronWord facilite l'automatisation du processus d'ajout d'images dans les fichiers DOCX, rationalisant ainsi les tâches de création et d'édition de documents.

Ahmad Sohail
Développeur Full Stack

Ahmad est un développeur full-stack avec une solide fondation en C#, Python et technologies web. Il a un profond intérêt pour la construction de solutions logicielles évolutives et aime explorer comment le design et la fonctionnalité se rencontrent dans des applications du monde réel.

<...
Lire la suite
Prêt à commencer?
Nuget Téléchargements 25,807 | Version : 2025.11 vient de sortir