文档元素教程
IronWord是一个功能强大的Word文档库,旨在帮助.NET C#开发者将创建、阅读和编辑Word及DOCX文档的能力整合到他们的应用程序中。 在Word文档的上下文中,文档元素是构成内容的基本构件。
目录
- 添加文本运行
- 文本内容 (添加、追加和拆分)
- 设置样式 (字体家族和大小、颜色、粗斜体、删除线、下划线、上标和下标)
- 嵌入图像
- 添加图片
- 加载图像 (文件和文件流)
- 设置自动换行
- 设置尺寸 (宽度和高度)
- 设置位置偏移
- 设置与角落的距离
开始使用IronZIP
立即在您的项目中开始使用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")