C# 文檔元素教學
IronWord 是一個功能強大的 Word 文件庫,旨在幫助 .NET C# 開發人員將建立、讀取和編輯 Word 和 DOCX 文件的功能整合到他們的應用程式中。 在 Word 文件中,文件元素是構成內容的基本組成部分。
快速入門:一次建立樣式文字並嵌入圖像
以下是如何使用 IronWord 快速添加豐富的內容——將樣式化的文字和嵌入的圖像組合在一個段落中,並將文件保存在一個流暢的程式碼區塊中。 非常適合想要立即上手而無需編寫樣板程式碼的開發人員。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronWord
複製並運行這段程式碼。
new WordDocument() .AddParagraph(new Paragraph(new TextContent("Hello IronWord!")).AddImage(new ImageContent("pic.png"))) .SaveAs("output.docx");部署到您的生產環境進行測試
目錄
-新增文字 -文字內容(新增、追加和分割) -設定樣式(字體和字號、顏色、粗體和斜體、刪除線、底線、上標和下標) -嵌入圖片 -新增圖片 -載入圖片(檔案和檔案流) -設定自動換行 -設定尺寸(寬度和高度) -設定位置偏移 -設定與角落的距離
新增文字運行
文字內容
Split 方法用於根據指定的分隔符將文字運行分割為較小的 TextRuns 清單。 這樣就可以對文件中的文字資訊進行組織和操作。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.csusing 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")造型
為 TextRuns 設定樣式,可讓您定義文字的視覺呈現。 這包括指定字體大小、顏色、樣式、刪除線、底線、上標和下標等屬性。 配置樣式可以提昇文件中文字的整體外觀。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.csusing 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")取得文字填滿顏色
除了設定樣式外,IronWord 還提供了一種獲取現有文件 RGBA 顏色值的方法,以保持樣式的一致性。
:path=/static-assets/word/content-code-examples/tutorials/rgba-color-value.csusing 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)我們首先透過初始化新的 WordDocument 來 import 該文件,然後存取 Paragraphs 陣列和 Texts 陣列,以擷取 Text 屬性。 然後, Text類別傳回FillColor來顯示文字現有顏色的 RGBA 值。
嵌入圖片
此功能可讓您在內容中無縫插入影像,從而增強文件的整體視覺吸引力和溝通效果。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.csusing 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.csusing 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.csusing 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")常見問題解答
如何使用 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 文檔,利用其全面的庫功能。






