文件元素教程
IronWord是一款強大的Word文件庫,旨在幫助.NET C#開發者將創建、閱讀和編輯Word和DOCX文件的功能整合到他們的應用程序中。 在 Word 文件的上下文中,文件元素是構成內容的基本組成部分。
目錄
- 新增文字
- 文字內容 (添加、追加和拆分)
- 設置樣式 (字體系列和大小、顏色、粗體和斜體、刪除線、底線、上標和下標)
- 嵌入圖像
- 添加圖片
- 載入圖片 (檔案與檔案流)
- 設置自動換行
- 設置尺寸 (寬度和高度)
- 設置位置偏移
- 設置與角落的距離
開始使用IronWord
立即在您的專案中使用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
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.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")
嵌入圖像
此功能讓您可以無縫地在內容中包含圖像,增強文件的整體視覺吸引力和溝通力。
: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;
// 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")