如何在 C# 中使用文件元素

C# 文檔元素教學

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

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

快速入門:一次建立樣式文字並嵌入圖像

以下是如何使用 IronWord 快速添加豐富的內容——將樣式化的文字和嵌入的圖像組合在一個段落中,並將文件保存在一個流暢的程式碼區塊中。 非常適合想要立即上手而無需編寫樣板程式碼的開發人員。

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

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

    PM > Install-Package IronWord

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

    new WordDocument()
      .AddParagraph(new Paragraph(new TextContent("Hello IronWord!")).AddImage(new ImageContent("pic.png")))
      .SaveAs("output.docx");
  3. 部署到您的生產環境進行測試

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

目錄

-新增文字 -文字內容(新增、追加和分割) -設定樣式(字體和字號、顏色、粗體和斜體、刪除線、底線、上標和下標) -嵌入圖片 -新增圖片 -載入圖片(檔案和檔案流) -設定自動換行 -設定尺寸(寬度和高度) -設定位置偏移 -設定與角落的距離

新增文字運行

文字內容

Split方法用於根據指定的分隔符號將文字運行分割成較小的 TextRun 清單。 這樣就可以對文件中的文字資訊進行組織和操作。

: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")
$vbLabelText   $csharpLabel

造型

設定 TextRuns 的樣式可以定義文字的視覺呈現方式。 這包括指定字體大小、顏色、樣式、刪除線、底線、上標和下標等屬性。 配置樣式可以提昇文件中文字的整體外觀。

: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
TextContent textRun = new TextContent();
textRun.Text = "Add text using IronWord";
textRun.Style = new TextStyle()
{
    TextFont = new Font()
    {
        FontFamily = "Caveat",
        FontSize = 72,
    },
    Color = Color.Red,
    IsBold = true,
    IsItalic = true,
    Underline = new Underline(),
    Strike = StrikeValue.Strike,
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddText(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Load docx
Private doc As New WordDocument("document.docx")

' Configure text
Private textRun As New TextContent()
textRun.Text = "Add text using IronWord"
textRun.Style = New TextStyle() With {
	.TextFont = New Font() With {
		.FontFamily = "Caveat",
		.FontSize = 72
	},
	.Color = Color.Red,
	.IsBold = True,
	.IsItalic = True,
	.Underline = New Underline(),
	.Strike = StrikeValue.Strike
}

Dim paragraph As New Paragraph()

' Add text
paragraph.AddText(textRun)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
$vbLabelText   $csharpLabel

取得文字填滿顏色

除了設定樣式外,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.FillColor;

// Print the filled color variable to the console
Console.WriteLine(filledColor);
Imports IronWord
Imports IronWord.Models
Imports System

' Open existing Word
Private doc As New WordDocument("Accent1TextThemcolor.docx")

Private content As TextContent = doc.Paragraphs(0).Texts(0)

' This will show the R G B A of the themecolor
Private filledColor = content.FillColor

' Print the filled color variable to the console
Console.WriteLine(filledColor)
$vbLabelText   $csharpLabel

我們首先透過初始化一個新的WordDocument來匯入文檔,然後存取Paragraphs數組和Texts數組來檢索Text屬性。 然後, Text類別傳回FillColor來顯示文字現有顏色的 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")
$vbLabelText   $csharpLabel

新增圖片

載入圖片

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

: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")
$vbLabelText   $csharpLabel

配置映像

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

:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

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

// Configure image
ImageContent image = new ImageContent("image.jpg");
image.WrapText = WrapText.Square;
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

' Load docx
Private doc As New WordDocument()

' Configure image
Private image As New ImageContent("image.jpg")
image.WrapText = WrapText.Square
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50

Dim position = 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")
$vbLabelText   $csharpLabel

常見問題解答

如何使用 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 文件。

Curtis Chau
技術作家

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

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 27,129 | Version: 2025.11 剛發表