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

立即開始在您的項目中使用 IronWord 並免費試用。

第一步:
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.

提示An 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

常見問題解答

如何使用 C# 為 DOCX 檔案中新增圖像?

您可以使用 IronWord,透過 C# 程式設計方式為 DOCX 檔案中新增影像。它可以讓您輕鬆地操作 Word 文檔,包括插入圖像。

使用 IronWord 可以在 DOCX 檔案中新增哪些類型的圖片?

IronWord 支援多種圖片格式,包括 JPEG、PNG、BMP 和 GIF,這些圖片可新增至 DOCX 檔案。

是否可以使用 IronWord 在 Word 文件中定位圖片?

是的,IronWord 可以精確控制 Word 文件中的圖片位置,讓您可以指定圖片的位置和對齊方式。

我可以使用 IronWord 將多張圖片加入到同一個 Word 文件中嗎?

當然。 IronWord支援在單一Word文件中加入多張圖片,從而實現複雜的文件佈局。

IronWord是否支援在頁首和頁尾新增圖片?

是的,IronWord 可用於在 Word 文件的頁首和頁尾中新增圖像,從而增強文件的設計效果。

IronWord如何處理影像縮放和調整大小?

IronWord 提供縮放和調整圖片大小的功能,以便在將圖片新增至 Word 文件時,請確保圖片能夠無縫地適應佈局。

我可以使用 IronWord 為 DOCX 檔案中的圖片加上標題嗎?

是的,IronWord 允許您為圖像添加標題,從而增強 Word 文件中的清晰度和上下文。

是否可以使用 IronWord 取代 Word 文件中的現有圖片?

IronWord 可讓您取代 Word 文件中的現有圖片,從而在文件更新和編輯方面提供更大的靈活性。

使用 IronWord 在 DOCX 檔案新增影像需要哪些系統需求?

IronWord 需要 .NET 框架支持,並且相容於 Windows、macOS 和 Linux 環境,用於為 DOCX 檔案添加映像。

我能否使用 IronWord 自動將影像新增至 DOCX 檔案的過程?

是的,IronWord 可以自動完成 DOCX 檔案中的影像新增流程,從而簡化文件建立和編輯任務。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 25,807 | 版本: 2025.11 剛剛發布