Run
内容
[
隐藏
]Run class
表示具有相同字体格式的一系列字符。
要了解更多信息,请访问使用文档进行编程文档文章。
public class Run : Inline
构造函数
| 姓名 | 描述 |
|---|---|
| Run(DocumentBase) | 初始化Run类. |
| Run(DocumentBase, string) | 初始化跑步类. |
特性
| 姓名 | 描述 |
|---|---|
| CustomNodeId { get; set; } | 指定自定义节点标识符。 |
| virtual Document { get; } | 获取此节点所属的文档。 |
| Font { get; } | 提供对此对象的字体格式的访问。 |
| virtual IsComposite { get; } | 返回真的如果此节点可以包含其他节点。 |
| IsDeleteRevision { get; } | 如果在启用更改跟踪的情况下在 Microsoft Word 中删除了此对象,则返回 true。 |
| IsFormatRevision { get; } | 如果在启用更改跟踪的情况下 Microsoft Word 中的对象格式发生更改,则返回 true。 |
| IsInsertRevision { get; } | 如果在启用更改跟踪的情况下将此对象插入 Microsoft Word,则返回 true。 |
| IsMoveFromRevision { get; } | 返回真的如果在启用更改跟踪的情况下在 Microsoft Word 中移动(删除)此对象。 |
| IsMoveToRevision { get; } | 返回真的如果在启用更改跟踪的情况下在 Microsoft Word 中移动(插入)此对象。 |
| IsPhoneticGuide { get; } | 获取一个布尔值,指示运行是否为语音指南。 |
| NextSibling { get; } | 获取紧随此节点之后的节点。 |
| override NodeType { get; } | 返回Run. |
| ParentNode { get; } | 获取此节点的直接父节点。 |
| ParentParagraph { get; } | 检索父级Paragraph此节点的。 |
| PhoneticGuide { get; } | 获得PhoneticGuide对象. |
| PreviousSibling { get; } | 获取此节点前一个节点。 |
| Range { get; } | 返回Range表示此节点中包含的文档部分的对象。 |
| Text { get; set; } | 获取或设置运行的文本。 |
方法
| 姓名 | 描述 |
|---|---|
| override Accept(DocumentVisitor) | 接受访客。 |
| Clone(bool) | 创建节点的副本。 |
| GetAncestor(NodeType) | 获取指定的第一个祖先NodeType. |
| GetAncestor(Type) | 获取指定对象类型的第一个祖先。 |
| override GetText() | 获取运行的文本。 |
| NextPreOrder(Node) | 根据前序树遍历算法获取下一个节点。 |
| PreviousPreOrder(Node) | 根据前序树遍历算法获取前一个节点。 |
| Remove() | 将自身从父级中移除。 |
| ToString(SaveFormat) | 将节点的内容导出为指定格式的字符串。 |
| ToString(SaveOptions) | 使用指定的保存选项将节点内容导出为字符串。 |
评论
文档的所有文本都以文本串的形式存储。
Run只能是Paragraph或内联StructuredDocumentTag。
例子
展示如何使用字体属性来格式化文本。
Document doc = new Document();
Run run = new Run(doc, "Hello world!");
Aspose.Words.Font font = run.Font;
font.Name = "Courier New";
font.Size = 36;
font.HighlightColor = Color.Yellow;
doc.FirstSection.Body.FirstParagraph.AppendChild(run);
doc.Save(ArtifactsDir + "Font.CreateFormattedRun.docx");
展示如何手动构建 Aspose.Words 文档。
Document doc = new Document();
// 一个空白文档包含一个部分、一个正文和一个段落。
// 调用“RemoveAllChildren”方法删除所有节点,
// 并最终得到一个没有子节点的文档节点。
doc.RemoveAllChildren();
// 此文档现在没有可以添加内容的复合子节点。
// 如果我们想要编辑它,我们将需要重新填充它的节点集合。
// 首先,创建一个新的部分,然后将其作为子节点附加到根文档节点。
Section section = new Section(doc);
doc.AppendChild(section);
// 为该部分设置一些页面设置属性。
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
// 一个部分需要一个主体,它将包含并显示其所有内容
// 位于页面部分页眉和页脚之间。
Body body = new Body(doc);
section.AppendChild(body);
// 创建一个段落,设置一些格式属性,然后将其作为子部分附加到正文中。
Paragraph para = new Paragraph(doc);
para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
body.AppendChild(para);
// 最后,添加一些内容来执行文档。创建一个运行,
// 设置其外观和内容,然后将其作为子项附加到段落。
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = Color.Red;
para.AppendChild(run);
Assert.AreEqual("Hello World!", doc.GetText().Trim());
doc.Save(ArtifactsDir + "Section.CreateManually.docx");
展示如何在 CompositeNode 的子节点集合中添加、更新和删除子节点。
Document doc = new Document();
// 默认情况下,空文档有一个段落。
Assert.AreEqual(1, doc.FirstSection.Body.Paragraphs.Count);
// 复合节点(例如我们的段落)可以包含其他复合节点和内联节点作为子节点。
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Run paragraphText = new Run(doc, "Initial text. ");
paragraph.AppendChild(paragraphText);
// 再创建三个运行节点。
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "Run 3. ");
// 在我们将它们插入到复合节点之前,文档主体不会显示这些运行
// 它本身是文档节点树的一部分,就像我们第一次运行一样。
// 我们可以确定插入节点的文本内容
// 通过指定相对于段落中另一个节点的插入位置出现在文档中。
Assert.AreEqual("Initial text.", paragraph.GetText().Trim());
// 将第二个运行插入到第一个运行前面的段落中。
paragraph.InsertBefore(run2, paragraphText);
Assert.AreEqual("Run 2. Initial text.", paragraph.GetText().Trim());
// 在初次运行后插入第三次运行。
paragraph.InsertAfter(run3, paragraphText);
Assert.AreEqual("Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
// 将第一个运行插入到段落子节点集合的开头。
paragraph.PrependChild(run1);
Assert.AreEqual("Run 1. Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(4, paragraph.GetChildNodes(NodeType.Any, true).Count);
// 我们可以通过编辑和删除现有的子节点来修改运行的内容。
((Run)paragraph.GetChildNodes(NodeType.Run, true)[1]).Text = "Updated run 2. ";
paragraph.GetChildNodes(NodeType.Run, true).Remove(paragraphText);
Assert.AreEqual("Run 1. Updated run 2. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);
也可以看看
- class Inline
- 命名空间 Aspose.Words
- 部件 Aspose.Words