文件元素教程
IronWord 是一個功能強大的 Word 文檔庫,旨在協助 .NET C# 開發人員將創建、閱讀和編輯 Word 和 DOCX 文檔的能力整合到他們的應用程序中。在 Word 文檔的上下文中,文件元素是組成內容的基本構件。
目錄
安裝與 NuGet
Install-Package IronWord
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronWord 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變。
Install-Package IronWord
添加文本運行
文本內容
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
Text addText = new Text("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
Text appendText = new Text("The first text.");
appendText.Append(new Text("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
Text splitText = new Text("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 Text("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New Text("The first text.")
appendText.Append(New Text("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New Text("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.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Text textRun = new Text();
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 Text()
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")
嵌入圖片
此功能允許您將圖片無縫地包含在內容中,增強文檔的整體視覺吸引力和溝通效果。
: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
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
Text textRun = new Text();
// 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 IronWord.Models.Image("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New Text()
' 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;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("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 IronWord.Models.Image("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")