如何在 C# 中將圖片加入 DOCX 檔案
IronWord 提供 ImageContent 類別,用於將圖片(.jpg、.png、.bmp、.tiff、.gif)插入 DOCX 檔案中,並可自訂寬度、高度及文字環繞等屬性。 使用 IronWord 將圖片加入 WORD 文件,以實現文件自動化與報表生成。
快速入門:使用 C# 將圖片加入 DOCX 檔案
- 透過 NuGet 套件管理員安裝 IronWord
- 建立一個新的
WordDocument實例 - 使用
ImageContent類別載入您的圖片 - 使用
AddImage()將圖片加入文件中 - 將文件儲存為 DOCX 格式
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
請複製並執行此程式碼片段。
using IronWord; using IronWord.Models; // Create new document WordDocument doc = new WordDocument(); // Add image ImageContent image = new ImageContent("photo.jpg"); doc.AddImage(image); // Save document doc.SaveAs("document-with-image.docx"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronWord
試用 IronWord
如何在 DOCX 檔案中加入圖片
- 下載 IronWord 的最新穩定版本
- 建立一個新的 WORD 文件
- 定義一個影像物件(.bmp、.jpg、.png 或其他受支援的格式)
- 將圖片加入文件中
- 儲存並匯出文件檔案
如何在 DOCX 檔案中加入圖片?
請使用檔案路徑來引用圖片。 首先,使用字串形式的檔案路徑來實例化 ImageContent 類別。 請在整個檔案中使用 image 變數來修改寬度和高度等屬性。 請使用 AddImage() 函式將圖片加入 .docx 檔案中。 將文件匯出並儲存至本地端。
以下範例將圖片新增至文件中,且該圖片沒有任何父節點。 支援的檔案格式包括 .jpg、.png、.bmp、.tiff 及 .gif。 這種靈活性讓您能夠處理任何常見的圖像格式。
: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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Initializing docx file
Dim doc As New IronWord.WordDocument()
' Instantiating image file
Dim image As 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")
支援哪些圖像格式?
支援的檔案格式:.jpg、.png、.bmp、.tiff 及 .gif。 每種格式在插入時均能維持其品質。 JPEG 最適合用於照片。PNG 支援透明度,適用於標誌和圖形。 BMP 提供未壓縮的畫質。 TIFF 適用於高品質的列印文件。 GIF 支援簡單的動畫效果(但在靜態文件中僅顯示第一幀)。
圖片在文件中的位置在哪裡?
圖片預設會新增至當前游標位置,且不帶任何父節點。 為精準定位,請將圖片插入段落內作為子元素。此舉可更有效地控制文字流向,並將圖片與文件結構整合。
為何要使用 ImageContent 類別?
ImageContent 類別以結構化的方式管理影像屬性。 插入前請先調整尺寸、位置及格式。 此方法可確保文件生成流程的一致性,並在整個應用程式中貫徹標準的格式規範。 該類別封裝了所有與影像相關的屬性,使程式碼更易於維護,並減少格式錯誤。
如何透過 Stream 添加圖片?
使用前述方法,可輕鬆加入本地或靜態 URL 圖片。 然而,應用程式通常會處理來自資料庫、網路服務或動態產生內容的圖片。 請使用 Stream 方法,在需要驗證的安全 API 後方插入圖片。
以下範例展示了一個 HTTP 客戶端,其傳送授權憑證以擷取經過驗證的影像串流。 該流程會在匯出前直接整合至文件中。 此方法可消除臨時檔案儲存的需求,並提升敏感影像資料的安全性。
: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;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
// 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 = await client.GetStreamAsync("https://api.example.com/secure/image.png");
doc.AddImage(authenticatedStream);
}
// Export docx
doc.SaveAs("added-image-via-http-stream.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
' initializing docx file
Dim doc As New IronWord.WordDocument()
Using client As 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
Dim authenticatedStream As Stream = Await client.GetStreamAsync("https://api.example.com/secure/image.png")
doc.AddImage(authenticatedStream)
End Using
' Export docx
doc.SaveAs("added-image-via-http-stream.docx")
何時該使用 Stream 方法?
在以下情況下請使用 Stream 方法:
- 圖片位於需經認證的安全 API 後端
- 從記憶體中動態處理影像
- 處理儲存於資料庫中的二進位資料格式圖片
此方法在Enterprise應用程式中運作良好,無論是將影像儲存於文件管理系統、雲端儲存空間,或是由影像處理服務所產生。
串流載入有哪些好處?
串流載入功能可從已驗證的端點整合圖片,無需節省臨時檔案。 這有助於提升安全性與效能。 優勢包括:
- 減少磁碟 I/O 操作
- 不會在磁碟上進行敏感影像的快取
- 即時影像處理工作流程
- 針對大型圖片提供更佳的記憶體管理
- 靈活的圖片來源選項
如何修改圖片屬性?
IronWord 提供全面的方法來自訂影像屬性。 請在將圖片加入文件之前或之後調整這些屬性。
| 設定 | 描述 | 範例 |
|---|---|---|
寬度 |
圖片的水平尺寸(單位:像素) | image.Width = 500; |
高度 |
圖片的垂直尺寸(單位:像素) | image.Height = 300; |
WrapText |
圖片周圍的文字換行行為 | image.WrapText = WrapText.Square; |
DistanceFromLeft |
以像素為單位,從左邊緣起計算的間距 | image.DistanceFromLeft = 10; |
DistanceFromRight |
以像素為單位,從右邊緣量取的間距 | image.DistanceFromRight = 10; |
距離頂部 |
以像素為單位,自頂邊緣起計算的間距 | image.DistanceFromTop = 15; |
DistanceFromBottom |
以像素為單位,自底部邊緣起算的間距 | image.DistanceFromBottom = 15; |
職位 |
空間定位資訊(X 和 Y 座標) | image.Position = new ElementPosition(50, 100); |
規模 |
X 軸與 Y 軸的比例縮放係數 | image.Scale = new PointF(1.5f, 1.5f); |
翻譯 |
重新定位的位移座標 | image.Translate = new PointF(20, 30); |
如何自訂寬度與高度?
透過調整長寬比來設定自訂寬度和高度。 控制圖片在文件中的顯示方式,無論是維持原始比例,還是配合特定的版面配置限制。
:path=/static-assets/word/content-code-examples/how-to/add-image-custom-size.cs
using IronWord;
// 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");
Imports IronWord
' initializing docx file
Dim doc As New IronWord.WordDocument()
' instantiating image file
Dim image As 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")
長寬比會如何變化?
自訂的寬度和高度值會覆寫原始的長寬比。 請拉伸或壓縮圖片,以符合頁首、側邊欄或固定尺寸容器等版面配置需求。 過度扭曲的翻譯可能會顯得不專業。 調整尺寸時,請根據目標尺寸計算比例尺寸以維持長寬比。
我應該先設定哪些屬性?
請按以下順序設定屬性:
- 尺寸(寬度/高度)—— 圖像佈局的基礎
- 定位 (DistanceFrom 屬性) — 控制間距與邊距
- 進階屬性(縮放/平移) - 微調
此方法可確保每個屬性都能在邏輯上延續前一個屬性。 某些屬性會相互影響——文字換行會影響距離屬性的運作方式。
常見問題
如何在 C# 中將圖片加入 DOCX 檔案?
透過 IronWord,您可以透過建立一個包含圖片檔案路徑的 ImageContent 實例,並使用 AddImage() 方法,將圖片新增至 DOCX 文件中。IronWord 支援 JPG、PNG、BMP、TIFF 和 GIF 等常見格式,讓您能輕鬆地透過程式碼將圖片插入 Word 文件中。
在 WORD 文件中插入圖片時,支援哪些圖片格式?
IronWord 支援所有主要圖像格式,包括 .jpg、.png、.bmp、.tiff 及 .gif 檔案。各格式在插入時皆能維持其品質——JPEG 適用於照片、PNG 適用於具透明度的圖形、BMP 適用於未壓縮的高畫質、TIFF 適用於高品質的 PRINT 文件,以及 GIF 適用於簡單的動畫(僅顯示第一幀)。
我能否控制 DOCX 檔案中圖片的大小與位置?
是的,IronWord 的 ImageContent 類別允許您自訂圖片屬性,例如寬度、高度及文字環繞方式。您可以在插入前修改尺寸和位置,從而完全掌控圖片在 WORD 文件中的顯示方式。
如何在段落中插入圖片以改善文件結構?
IronWord 允許您將圖片作為子元素插入段落中,從而提供更完善的文件層級結構及對文字換行更精確的控制。此方法能將圖片與周圍的文字流整合,相較於未設定父節點直接插入圖片,能提供更精確的定位選項。
要透過程式碼將圖片加入 WORD 文件,最快速的方法是什麼?
using IronWord 最快速的方法是建立一個 WordDocument 實例,透過 new ImageContent('photo.jpg') 載入圖片,呼叫 doc.AddImage(image),並使用 doc.SaveAs('document-with-image.docx') 儲存檔案。這個簡單的四步驟流程即可處理整個圖片插入工作流程。

