如何在C#中將圖像新增到DOCX
IronWord提供ImageContent類別,用於將圖像(.jpg、.png、.bmp、.tiff、.gif)插入到DOCX文件中,並可以自定義屬性如寬度、高度和文字環繞。 使用IronWord將圖像新增到Word文件中,以進行文件自動化和報告生成。
快速入門:在C#中將圖像新增到DOCX
- 通過NuGet程式庫管理員安裝IronWord
- 建立一個新的
WordDocument實例 - 使用
ImageContent類載入圖像 - 使用
AddImage()將圖像新增到文件中 - 將文件保存為DOCX
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/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類以結構化方式管理圖像屬性。 在插入之前修改尺寸、位置和格式。 這種方法確保了文件生成過程的一致性,並將標準格式規則應用於整個應用程式。 該類封裝了所有與圖像相關的屬性,使程式碼更易於維護並減少格式錯誤。
如何通過流的方式新增圖像?
本地或靜態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方法:
- 圖像在需要身份驗證的安全API後
- 從記憶體動態處理圖像
- 處理儲存在資料庫中的二進制資料圖像
此方法在企業應用程式中效果很好,特別是圖像儲存在文件管理系統、雲儲存中,或由圖像處理服務生成。
流載入的好處是什麼?
流載入從經過身份驗證的端點整合圖像,而不需要保存暫存檔。 這提高了安全性和性能。 好處包括:
- 減少磁碟I/O操作
- 沒有敏感圖像的磁碟快取
- 實時圖像處理工作流程
- 更好的大圖像記憶體管理
- 靈活的圖像來源選項
我如何修改圖像屬性?
IronWord提供全面的方法來自定義圖像屬性。 在將圖像新增到文件之前或之後調整這些屬性。
| 設定 | 描述 | 範例 |
|---|---|---|
Width |
圖像的水平方向像素尺寸 | image.Width = 500; |
Height |
圖像的垂直方向像素尺寸 | image.Height = 300; |
WrapText |
圖像的文字環繞行為 | image.WrapText = WrapText.Square; |
DistanceFromLeft |
從左邊緣到的距離測量,以像素為單位 | image.DistanceFromLeft = 10; |
DistanceFromRight |
從右邊緣到的距離測量,以像素為單位 | image.DistanceFromRight = 10; |
DistanceFromTop |
從上邊緣到的距離測量,以像素為單位 | image.DistanceFromTop = 15; |
DistanceFromBottom |
從下邊緣到的距離測量,以像素為單位 | image.DistanceFromBottom = 15; |
Position |
空間放置資訊(X和Y座標) | image.Position = new ElementPosition(50, 100); |
Scale |
X和Y軸比例調整因子 | image.Scale = new PointF(1.5f, 1.5f); |
Translate |
重新定位的位移座標 | 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用於高質量印刷文件,GIF用於簡單的動畫(僅顯示第一幀)。
我可以控制DOCX文件中圖像的大小和位置嗎?
是的,IronWord的ImageContent類讓您可以自定義圖像屬性,例如寬度、高度和文字環繞。您可以在插入前修改尺寸和位置,讓您對Word文件中圖像的顯示有完全控制。
如何在段落中插入圖像以改善文件結構?
IronWord允許您將圖像作為段落的子元素插入,提供更好的文件層次和對文字環繞的控制。這種方式讓圖像與周圍文字的流動結合,比沒有父節點新增圖像提供更精確的定位選項。
什麼是最快的程式方式將圖像新增到Word文件的方法?
使用IronWord,最快的方法是建立一個WordDocument實例,使用新的ImageContent('photo.jpg')載入您的圖像,調用doc.AddImage(image),並用doc.SaveAs('document-with-image.docx')保存。這個簡單的四步驟過程處理了整個圖像插入工作流程。

