C# 文件元素教學

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronWord 是一個功能強大的 Word 文件庫,旨在幫助 .NET C# 開發人員將建立、讀取和編輯 Word 和 DOCX 文件的功能整合到他們的應用程式中。 在 Word 文件中,文件元素是構成內容的基本組成部分。

快速入門:建立樣式化 TextRun 並嵌入圖片

以下是如何使用IronWord在文件中添加豐富內容 - 結合樣式化文本和嵌入影像。 此示範展示了Run樣式化文本的包裝模式。

  1. 1使用 IronWord 安裝 NuGet Package Manager

    PM > Install-Package IronWord

  2. 2複製並運行這段程式碼。

    using IronWord;
    using IronWord.Models;
    
    // Create document
    WordDocument doc = new WordDocument();
    
    // Create styled Run
    Run textRun = new Run(new TextContent("Hello IronWord!"));
    textRun.Style = new TextStyle() { IsBold = true, Color = Color.Blue };
    
    // Create paragraph and add Run
    Paragraph paragraph = new Paragraph();
    paragraph.AddChild(textRun);
    paragraph.AddImage(new ImageContent("pic.png"));
    
    // Add to document and save
    doc.AddParagraph(paragraph);
    doc.SaveAs("output.docx");
  3. 3部署到您的生產環境進行測試

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

目錄

關鍵概念

文件層級結構

IronWord遵循結構化文件層次:Document → Paragraph → Run → TextContent。 理解此層級結構對於處理格式化文字至關重要:

  • WordDocument:代表整個文件的最高層容器
  • Paragraph:文件內的一個內容區塊
  • Run:包含TextContent的包裝物件,可以套用樣式
  • TextContent:實際的文本字符串

在段落中添加內容

IronWord 提供兩種向段落中添加內容的方法:

  • AddText(TextContent):用於無樣式的純文本。 直接將TextContent添加到段落。
  • AddChild(Run):用於樣式化文本。 將Run對象(它包裝TextContent並保存樣式)添加到段落中。

當您需要應用字體大小、顏色或粗體格式等樣式時,請建立一個Run對象,為Run指定TextStyle,然後使用AddChild將其添加到段落中。

新增文字運行

文字內容

Split方法用於根據指定的分隔符將文本運行拆分成一系列較小的文本運行。 這樣就可以對文件中的文字資訊進行組織和操作。

using IronWord;
using IronWord.Models;

WordDocument doc = new WordDocument();

// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));

// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));

// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));

// Export docx
doc.SaveAs("textrun.docx");

造型

為文本設定樣式需要使用Run包裝模式。 創建一個Run對象,包含TextContent,然後為Run指定TextStyle(而不是TextContent)。 TextStyle允許您定義視覺屬性,例如FontSize,顏色,加粗,斜體,刪除線,下劃線,上標和下標。

請注意,FontSize是在TextStyle級別配置,而FontFamily是在TextFont屬性內設置。 在樣式設置後,使用AddChild將Run添加到段落中。 這遵循文件層次:Document → Paragraph → Run → TextContent。

using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
    FontSize = 72,
    TextFont = new Font()
    {
        FontFamily = "Caveat"
    },
    Color = Color.Red,
    IsBold = true,
    IsItalic = true,
    Underline = new Underline(),
    Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddChild(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");

取得文字填滿顏色

除了設定樣式之外,IronWord還提供了一種方法來從現有文件中獲取RGBA顏色值,以保持樣式的一致性。

using IronWord;
using IronWord.Models;
using System;

// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");

TextContent content = doc.Texts[0];

// This will show the R G B A of the themecolor
var filledColor = content.Color;

// Print the filled color variable to the console
Console.WriteLine(filledColor);

要檢索顏色值,請使用WordDocument載入文件,然後訪問Texts集合以獲取TextContent對象。 TextContent的Color屬性返回文字現有顏色的RGBA值,允許您在整個文件中保持一致的樣式。

嵌入圖片

此功能允許您在內容中包含影像,增強整體視覺吸引力和文件的溝通力。

using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument();

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();

// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);

// Add paragraph
doc.AddParagraph(para);

// Export docx
doc.SaveAs("save_document.docx");

新增圖片

載入圖片

圖片載入是一個至關重要的過程。 這涉及到將外部圖像檔案匯入到文件中。 載入圖像的功能有助於添加相關的視覺元素,從而使文件更具吸引力和資訊量。

using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument();

Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage("image.jpg");

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("document.docx");

配置映像

使用可配置設定優化影像。 這包括設定如尺寸、位置和與角落距離的屬性。 正確的配置可確保影像以賞心悅目且符合上下文的方式顯示。

請注意: 圖像還公開了一個TextWrapBehavior屬性(類型ITextWrapBehavior),用於控制圖像周圍的文字環繞,但目前未記錄任何公有實現類或工廠用於構造值 - 此示例先暫時省略,等待產品團隊確認。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Load docx
WordDocument doc = new WordDocument();

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;

var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;

Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage(image);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("document.docx");

常見問題

IronWord在C#開發中用於什麼?

IronWord是一個強大的.NET C#開發者程式庫,旨在幫助在應用程式中創建、讀取和編輯Word和DOCX文件。

如何使用IronWord將樣式化文字添加到Word文件中?

您可以通過創建包裝'文本內容'的'Run'物件來添加樣式化文字,然後應用一個'TextStyle'到它,並使用IronWord的'AddChild'方法將其添加到段落中。

在IronWord中,'Run'物件的目的為何?

'Run'物件在IronWord中用作'TextContent'的包裝器,允許對文字進行樣式化設置,如字體大小、顏色等屬性。

如何使用IronWord將圖像嵌入到文檔中?

要嵌入圖像,需創建一個'ImageContent'物件,並使用'AddImage'方法將其添加到段落中。這使您可以將圖像插入文檔中。

在IronWord中,設置圖像時涉及哪些步驟?

設置圖像涉及到設置屬性,如尺寸、位置和與角落的距離,確保圖像在文檔中正確顯示。

IronWord能否從現有的Word文檔中檢索顏色值?

可以,IronWord可以通過'TextContent'的'Color'屬性從現有文檔中檢索RGBA顏色值,幫助維持樣式一致性。

IronWord使用什麼方法來分割一個文本運行?

IronWord使用'Split'方法根據指定的分隔符將一個文本運行分割成較小的運行,以便更好地組織和操作文本內容。

為什麼理解文檔層次結構在IronWord中很重要?

理解文檔層次結構(文檔→段落→運行→文本內容)對於使用IronWord有效管理和樣式化Word文檔內容非常重要。

如何保存使用IronWord創建的Word文檔?

使用IronWord創建的Word文檔可以使用'SaveAs'方法進行保存,指定用於保存文檔的檔名和路徑。

'AddParagraph'方法在IronWord中的功能是什麼?

'AddParagraph'方法在IronWord中被用於將完全配置的段落與文字和圖像一起添加到Word文檔中。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 63,050版本:2026.11剛剛發布