C# Document Element Tutorial
IronWord 是一個功能強大的 Word 文件庫,旨在幫助 .NET C# 開發人員將建立、讀取和編輯 Word 和 DOCX 文件的功能整合到他們的應用程式中。 在 Word 文件中,文件元素是構成內容的基本組成部分。
快速入門:建立樣式化 TextRun 並嵌入圖片
以下是您如何使用 IronWord 來添加豐富內容——在文件中結合格式化文字與嵌入式圖片。 此範例展示了 Run 包裹模式在格式化文字中的應用。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/IronWord
PM > Install-Package IronWord -
複製並運行這段程式碼。
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"); -
部署到您的生產環境進行測試
今天就在您的專案中開始使用免費試用IronWord
目錄
-新增文字 -文字內容(新增、追加和分割) -設定樣式(字體和字號、顏色、粗體和斜體、刪除線、底線、上標和下標) -嵌入圖片 -新增圖片 -載入圖片(檔案和檔案流) -設定自動換行 -設定尺寸(寬度和高度) -設定位置偏移 -設定與角落的距離
主要概念
文件層級結構
IronWord 遵循結構化的文件層級:文件 → 段落 → 執行區段 → 文字內容。 理解此層級結構對於處理格式化文字至關重要:
WordDocument:代表整個文件的頂層容器Paragraph:文件中的內容區塊Run:一個包含TextContent並可套用樣式的封裝物件TextContent: 實際文字字串
在段落中添加內容
IronWord 提供兩種向段落中添加內容的方法:
AddText(TextContent):用於未格式化的純文字。 將TextContent直接加入該段落中。AddChild(Run):用於格式化文字。 在段落中新增一個Run物件(該物件包覆TextContent並儲存樣式)。
當您需要套用字型大小、顏色或粗體等格式時,請建立一個 Run 物件,將 TextStyle 指派給 Run,並使用 AddChild 將其加入段落中。
新增文字運行
文字內容
Split 方法用於根據指定的分隔符,將文本段落分割成一系列較小的文本段落。 這樣就可以對文件中的文字資訊進行組織和操作。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
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");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")
造型
設定文字樣式時,請使用 Run 包覆模式。 建立一個包含 TextContent 的 Run 物件,然後將 TextStyle 賦值給 Run(而非 TextContent)。 TextStyle 讓您能夠定義視覺屬性,例如 FontSize、顏色、粗體、斜體、刪除線、底線、上標和下標。
請注意,FontSize 是在 TextStyle 層級進行設定,而 FontFamily 則設定於 TextFont 屬性之中。 格式化完成後,請使用 AddChild 將 Run 加入段落中。 此文件遵循以下層級結構:文件 → 段落 → 執行 → 文字內容。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
取得文字填滿顏色
除了設定樣式外,IronWord 還提供一種方法,讓您能從現有文件中取得 RGBA 顏色值,以確保整體樣式的一致性。
:path=/static-assets/word/content-code-examples/tutorials/rgba-color-value.cs
using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Paragraphs[0].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);
Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Paragraphs(0).Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)
要擷取顏色值,請使用 WordDocument 載入文件,接著存取 Paragraphs 集合與 Texts 陣列,以取得 TextContent 物件。 Color 的 TextContent 屬性會傳回文字現有顏色的 RGBA 值,讓您能在整個文件中維持一致的樣式。
嵌入圖片
此功能可讓您在內容中無縫插入影像,從而增強文件的整體視覺吸引力和溝通效果。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
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(new Paragraph(textRun));
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Configure image
Private image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(New Paragraph(textRun))
' Export docx
doc.SaveAs("save_document.docx")
新增圖片
載入圖片
圖片載入是一個至關重要的過程。 這涉及到將外部圖像檔案匯入到文件中。 載入圖像的功能有助於添加相關的視覺元素,從而使文件更具吸引力和資訊量。
:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
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");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
配置映像
使用可配置設定優化影像。 這包括設定文字環繞、尺寸、位置和距角點的距離等屬性。 正確的配置可確保影像以賞心悅目且符合上下文的方式顯示。
:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using IronSoftware.Abstractions.Word;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.TextWrapBehavior = new TextWrapSquare();
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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports IronSoftware.Abstractions.Word
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.TextWrapBehavior = New TextWrapSquare()
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
常見問題解答
如何使用 C# 向 Word 文檔添加文本?
您可以使用 IronWord 創建一個文本段的實例,然後將其附加到文檔內的段落中來向 Word 文檔中添加文本。
在 Word 文檔中分割文本的方法是什麼?
IronWord 提供了 Split 方法,允許您根據指定的分隔符將文本段分割為更小的部分,以便更容易進行文本操作。
如何使用 IronWord 在 Word 文檔中設置文本樣式?
您可以通過設置各種屬性來使用 IronWord 設置文本樣式,例如字體大小,顏色和粗體、斜體、刪除線、下劃線、上標和下標等樣式。
如何使用 C# 在 Word 文檔中嵌入圖片?
要使用 IronWord 在 Word 文檔中嵌入圖片,您可以從文件中加載圖片,並將其作為內聯圖片添加到文檔的段落中。
加載圖片到 Word 文檔的步驟有哪些?
使用 IronWord,您可以從文件或文件流中加載圖片,這使您可以將視覺內容納入 Word 文檔中。
如何在 Word 文檔中配置圖片屬性?
使用 IronWord,您可以配置圖片屬性,如文本環繞、尺寸、位置偏移以及與角落的距離,以確保圖片在文檔中的正確顯示。
我可以檢索 Word 文檔中文字的 RGBA 顏色值嗎?
是的,IronWord 允許您獲得文檔中已有文本的 RGBA 顏色值,以保持一致的樣式。
如何開始使用 IronWord 來操作 Word 文檔?
要開始使用 IronWord,您可以將其集成到您的 .NET C# 應用程序中,以創建、讀取和編輯 Word 文檔,利用其全面的庫功能。

