如何在 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 方法用於根據指定的分隔符將文字運行分割為較小的 TextRuns 清單。 這樣就可以對文件中的文字資訊進行組織和操作。

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

我們首先透過初始化新的 WordDocumentimport 該文件,然後存取 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");
$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");
$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");
$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擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 29,594 | 版本: 2025.12 剛剛發布