如何在 C# 中將圖片新增至 DOCX
IronWord 提供 ImageContent 類別,可將圖片(.jpg、.png、.bmp、.tiff、.gif)插入 DOCX 檔案,並自訂寬度、高度和文繞圖等屬性。使用 IronWord 在 Word 文件中新增圖片,可自動建立文件及產生報表。
- 透過 NuGet 套件管理員安裝 IronWord
- 建立新的
WordDocument執行個體 - 使用
ImageContent類別載入圖片 - 使用
AddImage()將圖片新增至文件 - 將文件儲存為 DOCX
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
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");Imports IronWord Imports IronWord.Models ' Create new document Dim doc As New WordDocument() ' Add image Dim image As New ImageContent("photo.jpg") doc.AddImage(image) ' Save document doc.SaveAs("document-with-image.docx")Imports IronWord Imports IronWord.Models ' Create new document Dim doc As New WordDocument() ' Add image Dim image As New ImageContent("photo.jpg") doc.AddImage(image) ' Save document doc.SaveAs("document-with-image.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
試用 IronWord
如何在 DOCX 檔案中新增圖片
- 下載最新的 IronWord 穩定版
- 初始化新的 Word 文件
- 定義圖片物件(.bmp、.jpg、.png 或其他支援的格式)
- 將圖片加入文件中
- 儲存並匯出文件
如何將圖片新增至 DOCX?
使用圖片的檔案路徑來參照圖片。先將路徑字串傳入 ImageContent 的建構函式,再使用 image 變數修改寬度、高度等屬性。使用 AddImage() 方法將圖片新增至 .docx 檔案,然後匯出文件並儲存在本機。
下方範例將圖片直接新增至文件,無須指定父節點。支援 .jpg、.png、.bmp、.tiff 和 .gif,可使用常見的圖片格式。
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");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")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 的圖片。應用程式也常處理來自資料庫、網路服務或動態產生的圖片。若要新增須經身分驗證才能透過安全 API 取得的圖片,可使用 Stream 方式。
下方範例示範 HTTP 用戶端如何傳送授權權杖來擷取須經驗證才能存取的圖片串流。匯出前,程式會直接將串流中的圖片插入文件,省去儲存暫存檔的步驟,並提高敏感圖片資料的安全性。
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");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")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、Height和DistanceFrom*屬性在直接指定時默認為點(1英吋=72點),如下所示。 要使用不同的單位,請使用對應的Get/Set超載方法——例如,image.SetWidth(5, MeasurementUnit.Centimeter).| 設定 | 說明 | 範例 |
|---|---|---|
Width | 圖片的水平尺寸,以點為單位(1 英吋 = 72 點) | image.Width = 500;。 |
Height | 圖片的垂直尺寸,以點為單位(1 英吋 = 72 點) | image.Height = 300;。 |
TextWrapBehavior | 圖片周圍的文繞圖方式。此屬性採用 ITextWrapBehavior 介面。目前文件尚未列出可用來建立此屬性值的公開實作類別或工廠,因此在產品團隊確認前,暫不提供可執行的範例。 | - |
DistanceFromLeft | 與左側邊緣的距離,以點為單位 | image.DistanceFromLeft = 10;。 |
DistanceFromRight | 與右側邊緣的距離,以點為單位 | image.DistanceFromRight = 10;。 |
DistanceFromTop | 與上方邊緣的距離,以點為單位 | image.DistanceFromTop = 15;。 |
DistanceFromBottom | 與下方邊緣的距離,以點為單位 | image.DistanceFromBottom = 15;。 |
Position | 位置資訊(X 和 Y 座標) | var pos = new ElementPosition(); pos.SetXPosition(50, MeasurementUnit.Point); pos.SetYPosition(100, MeasurementUnit.Point); image.Position = pos; |
如何自訂寬度與高度?
您可以調整寬高比來自訂圖片的寬度與高度,控制圖片在文件中的呈現方式,以維持原有比例或符合版面限制。
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");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")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 屬性)可控制間距與邊界
- 進階屬性(位置與文繞圖)可用於微調
依此順序設定,可讓每個屬性以前一步的設定為基礎。有些屬性會互相影響,例如文繞圖會影響距離屬性的運作方式。
常見問題
如何使用IronWord將圖像添加到DOCX文件中?
要使用IronWord將圖像添加到DOCX文件中,首先創建一個WordDocument。使用ImageContent類載入您的圖像,必要時配置圖像屬性如寬度和高度,然後使用AddImage()方法將其插入到文件中。
IronWord支持哪些圖像格式用於DOCX文件?
IronWord支持多種圖像格式,包括.jpg、.png、.bmp、.tiff和.gif。這使您能夠在插入不同類型的圖像到您的DOCX文件中時保持圖像質量。
在將圖像添加到DOCX之前,可以在IronWord中自定義圖像尺寸嗎?
可以,您可以在將圖像添加到DOCX文件之前通過設置ImageContent類的Width和Height屬性來自定義圖像尺寸。
如何在DOCX中使用IronWord處理來自身份驗證API的圖像插入?
在IronWord中使用Stream方法從身份驗證API中加載圖像。此方法允許您使用安全、即時的處理方式將圖像直接整合到DOCX文件中,而不需要臨時保存圖像文件。
使用`Stream`方法加載IronWord中的圖像有什麼好處?
Stream方法通過減少磁碟I/O、避免臨時文件存儲、啟用實時工作流程、並改善大型圖像的記憶體管理,增強了安全性和性能。
如何在使用IronWord的DOCX文件內將圖像放置在特定位置?
IronWord中的圖像初始放置於當前游標位置。為了精確定位,將圖像作為段落的子元素插入,以便於控制文本流和佈局。
我能否修改IronWord中圖像的文本環繞設定?
可以,IronWord允許您使用TextWrapBehavior屬性設置圖像的文本環繞。這控制了文本在DOCX文件中插入圖像的環繞方式。
使用IronWord中的`ImageContent`類有什麼好處?
IronWord中的ImageContent類提供了一種結構化的方式來管理和自定義圖像屬性,如尺寸和位置,以確保在添加到DOCX文件中時的一致性,並減少格式錯誤。
如何在IronWord中調整圖像大小時保持其縱橫比?
為了在IronWord中調整圖像大小時保持縱橫比,根據目標大小計算比例尺寸,以避免失真並創造專業的展示效果。
在IronWord中設置圖像屬性應遵循什麼順序?
建議在IronWord中按以下順序設置屬性:首先設置尺寸(寬度/高度),然後是間距和邊距(DistanceFrom屬性),最後是進階設置如位置和文本環繞,以全面控制圖像布局。
Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。