如何在 C# 中向 DOCX 添加圖像

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

IronWord 提供 ImageContent 類別,可將圖片 (.jpg、.png、.bmp、.tiff、.gif) 插入 DOCX 檔案,並可自訂寬度、高度和文字包覆等屬性。 使用 IronWord 在 Word 文件中添加圖片,以實現文件自動化和報告生成。

快速入門:在 C# 中為 DOCX 添加圖片

1.透過 NuGet 套件管理員安裝 IronWord 2.建立新的 WordDocument 範例 3.使用 ImageContent 類載入您的圖片 4.使用 AddImage() 將圖片加入文件中 5.將檔案儲存為 DOCX

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronWord

    PM > Install-Package IronWord

  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");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronWord,免費試用!
    arrow pointer

試試IronWord


如何在 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")
$vbLabelText   $csharpLabel
Word 文件顯示插入的夜間城市天際線圖片,以及顯示格式選項的首頁選項帶

支援哪些影像格式?

支援的檔案格式:.jpg, .png, .bmp, .tiff 和 .gif。 每種格式在插入時都要保持其品質。 JPEG 最適用於照片。PNG 支援透明度,適用於標誌和圖形。 BMP 提供未壓縮的品質。 TIFF 適合高品質的列印文件。 GIF 允許簡單的動畫(在靜態文件中只顯示第一幀)。

圖片在文件中的位置?

預設會在目前游標位置新增圖片,不含任何父節點。 為了精確定位,請在段落中插入圖片作為子元素。這樣可以更好地控制文字流程,並將圖片與您的文件結構整合在一起。

為何使用 ImageContent 類別?

ImageContent 類以結構化的方式管理影像屬性。 插入前修改尺寸、定位和格式。 此方法可確保文件生成流程的一致性,並在整個應用程式中應用標準格式規則。 該類型封裝了所有與影像相關的屬性,使程式碼更易於維護,並減少格式錯誤。

如何透過 Stream 新增圖片?

使用前述方法可輕鬆新增本地或靜態 URL 圖片。 然而,應用程式通常會使用資料庫、Web 服務或動態產生內容的影像。 使用 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;

// 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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports System.Net.Http

' 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 = client.GetStreamAsync("https://api.example.com/secure/image.png").Result
    doc.AddImage(authenticatedStream)
End Using

' Export docx
doc.SaveAs("added-image-via-http-stream.docx")
$vbLabelText   $csharpLabel

何時應該使用 Stream 方法?

使用 Stream 方法時:

  • 圖片位於需要驗證的安全 API 之後
  • 從記憶體動態處理影像
  • 處理資料庫中儲存為二進位資料的影像

此方法適用於企業應用程式,在這些應用程式中,影像儲存在文件管理系統、雲端儲存或由影像處理服務所產生。

Stream Loading 有哪些優點?

串流載入可整合來自驗證端點的影像,而無需儲存臨時檔案。 這可提高安全性和效能。 福利包括:

  • 減少磁碟 I/O 作業
  • 磁碟上沒有敏感的影像快取
  • 即時影像處理工作流程
  • 針對大型影像提供更好的記憶體管理
  • 靈活的圖片來源選項

如何修改圖片屬性?

<! -- 說明修改影像屬性實作的圖表 --> <!--說明:說明程式碼概念的圖表或截圖 -->

IronWord 提供自訂影像屬性的全面方法。 在將影像加入文件之前或之後,調整這些屬性。

設定 描述 例子
Width 影像的水平尺寸(以像素為單位) image.Width=500;
Height 影像的垂直尺寸(以像素為單位) image.Height=300;
環繞文字 圖片周圍的文字環繞行為 image.環繞文字=環繞文字.Square;
距離左側 從左邊緣到間距的測量值(以像素為單位) image.距離左側=10;
距離 從右邊緣測量間距(以像素為單位) image.距離=10;
距離頁首 從頂部邊緣測量間距(以像素為單位) image.距離頁首=15;
距離底部 從底部邊緣測量間距(以像素為單位) image.距離底部=15;
位置 空間位置資訊(X 和 Y 座標) image.位置 = new Element位置(50, 100);
規模 X軸和Y軸的比例尺寸因子 image.規模 = new PointF(1.5f, 1.5f);
翻譯 用於重新定位的位移座標 image.翻譯 = new PointF(20, 30);

如何自訂寬度與高度?

透過改變寬高比來實現自訂寬度與高度。 控制圖片在文件中的顯示方式,無論是維持比例或符合特定的版面限制。

: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");
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.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")
$vbLabelText   $csharpLabel
顯示筆記型電腦圖片的 Word 文件,附有 'Tagged Image File F' 疊加文字,展示圖片屬性顯示

寬高比會發生什麼變化?

自訂的寬度與高度值會覆蓋原始的寬高比。 拉伸或壓縮圖片以符合版面需求,例如標題、側邊欄或固定大小的容器。 極度失真會顯得不專業。 為了在調整大小時維持長寬比,請根據您的目標尺寸計算比例尺寸。

我應該先設定哪些屬性?

依此順序設定屬性: 1.尺寸 (寬/高) - 影像排版的基礎 2.定位 (DistanceFrom 屬性) - 控制間距和邊距 3.進階屬性 (縮放/翻譯) - 微調

此方法可確保每項屬性都能順理成章地建立在先前屬性的基礎上。 某些屬性會互動 - 文字包裝會影響距離屬性的運作方式。

<!--工作後加入陰影造型-->。

常見問題解答

如何使用 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 實例,使用 new ImageContent('photo.jpg「) 載入您的圖片,呼叫 doc.AddImage(image),然後以 doc.SaveAs(」document-with-image.docx') 儲存。這個簡單的四步程序處理了整個圖像插入工作流程。

Ahmad Sohail
全堆疊開發人員

Ahmad 是一名全堆疊開發人員,在 C#、Python 和 Web 技術方面有深厚的基礎。

在加入 Iron Software 團隊之前,Ahmad 從事自動化專案和 API 整合工作,專注於改善效能和開發人員體驗。

在空閒時間,他喜歡嘗試 UI/UX 想法,為開源工具貢獻心力,偶爾也會鑽研技術撰寫和文件,讓複雜的主題更容易理解。

準備好開始了嗎?
Nuget 下載 32,629 | 版本: 2026.2 剛剛發布