使用样式和主题

StyleCollection 类用于管理内置样式并将用户定义的设置应用于样式。

访问样式

您可以使用 Document.Styles 属性获取文档中定义的样式集合。该集合包含文档中的内置样式和用户定义样式。特定样式可以通过其名称/别名、样式标识符或索引来获取。以下代码示例演示如何访问文档中定义的样式集合。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Load the template document.
Document doc = new Document(dataDir + "TestFile.doc");
// Get styles collection from document.
StyleCollection styles = doc.Styles;
string styleName = "";
// Iterate through all the styles.
foreach (Style style in styles)
{
if (styleName == "")
{
styleName = style.Name;
}
else
{
styleName = styleName + ", " + style.Name;
}
}

如何根据样式提取内容

简单地说,从 Word 文档中检索基于样式的内容对于识别、列出和计数段落以及使用特定样式格式化的文本很有用。例如,您可能需要识别文档中特定类型的内容,例如示例、标题、参考文献、关键字、图形名称和案例研究。

更进一步,这还可以用于利用由其使用的样式定义的文档结构,将文档重新用于其他输出,例如 HTML。事实上,这就是 Aspose 文档的构建方式,将 Aspose.Words 进行测试。使用 Aspose.Words 构建的工具获取源 Word 文档并将它们分成特定标题级别的主题。 XML 文件是使用 Aspose.Words 生成的,用于构建您可以在左侧看到的导航树。然后 Aspose.Words 将每个主题转换为 HTML。

使用 Aspose.Words 来检索 Word 文档中特定样式的文本格式的解决方案通常既经济又简单。

解决方案

为了说明 Aspose.Words 如何轻松地处理基于样式的内容检索,让我们看一个示例。在此示例中,我们将从示例 Word 文档中检索使用特定段落样式和字符样式格式化的文本。在较高级别上,这将涉及:# 使用 Document 类打开 Word 文档。# 获取集合文档中的所有段落和所有运行。# 仅选择所需的段落和运行。具体来说,我们将从这个示例 Word 文档中检索使用"标题 1"段落样式和"强烈强调"字符样式格式化的文本。

working-with-styles-and-themes-aspose-words-net

在此示例文档中,使用"标题 1"段落样式格式化的文本是"插入选项卡"、“快速样式"和"主题”,使用"强烈强调"字符样式格式化的文本是蓝色、斜体、粗体文本,例如"画廊"和"整体外观"。

代码

在 Aspose.Words 文档对象模型中,基于样式的查询的实现非常简单,因为它只使用已有的工具。此解决方案实现了两个类方法:ParagraphsByStyleName – 此方法检索这些段落的数组具有特定样式名称的文档。RunsByStyleName – 此方法检索文档中具有特定样式名称的运行的数组。这两种方法非常相似,唯一的区别是节点类型和样式的表示形式段落和运行节点中的信息。这是 ParagraphsByStyleName 的实现:下面的示例查找使用指定样式格式化的所有段落。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static ArrayList ParagraphsByStyleName(Document doc, string styleName)
{
// Create an array to collect paragraphs of the specified style.
ArrayList paragraphsWithStyle = new ArrayList();
// Get all paragraphs from the document.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
// Look through all paragraphs to find those with the specified style.
foreach (Paragraph paragraph in paragraphs)
{
if (paragraph.ParagraphFormat.Style.Name == styleName)
paragraphsWithStyle.Add(paragraph);
}
return paragraphsWithStyle;
}

还值得指出的是,段落集合不会立即产生开销,因为只有当您访问其中的项目时,段落才会加载到该集合中。然后,您需要做的就是使用标准 foreach 运算符遍历集合,并将具有指定样式的段落添加到 paragraphsWithStyle 数组中。 Paragraph 样式名称可以在 Paragraph.ParagraphFormat 对象的 Style.Name 属性中找到。尽管我们显然使用 NodeType.Run 来检索运行节点,但 RunsByStyleName 的实现几乎相同。 Run 对象的 Font.Style 属性用于访问 Run 节点中的样式信息。下面的示例查找使用指定样式格式化的所有运行。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static ArrayList RunsByStyleName(Document doc, string styleName)
{
// Create an array to collect runs of the specified style.
ArrayList runsWithStyle = new ArrayList();
// Get all runs from the document.
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
// Look through all runs to find those with the specified style.
foreach (Run run in runs)
{
if (run.Font.Style.Name == styleName)
runsWithStyle.Add(run);
}
return runsWithStyle;
}

当两个查询都实现时,您所需要做的就是传递一个文档对象并指定要检索的内容的样式名称:下面的示例运行查询并显示结果。您可以从这里下载本示例的模板文件。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithStyles();
string fileName = "TestFile.doc";
// Open the document.
Document doc = new Document(dataDir + fileName);
// Define style names as they are specified in the Word document.
const string paraStyle = "Heading 1";
const string runStyle = "Intense Emphasis";
// Collect paragraphs with defined styles.
// Show the number of collected paragraphs and display the text of this paragraphs.
ArrayList paragraphs = ParagraphsByStyleName(doc, paraStyle);
Console.WriteLine(string.Format("Paragraphs with \"{0}\" styles ({1}):", paraStyle, paragraphs.Count));
foreach (Paragraph paragraph in paragraphs)
Console.Write(paragraph.ToString(SaveFormat.Text));
// Collect runs with defined styles.
// Show the number of collected runs and display the text of this runs.
ArrayList runs = RunsByStyleName(doc, runStyle);
Console.WriteLine(string.Format("\nRuns with \"{0}\" styles ({1}):", runStyle, runs.Count));
foreach (Run run in runs)
Console.WriteLine(run.Range.Text);

最终结果

一切完成后,运行示例将显示以下输出:

styles-and-themes-aspose-words-net

正如您所看到的,这是一个非常简单的示例,显示了示例 Word 文档中收集的段落和运行的数量和文本。

从模板复制所有样式

有时您想要将一个文档中的所有样式复制到另一个文档中。您可以使用 Document.CopyStylesFromTemplate 方法将指定模板中的样式复制到文档中。将样式从模板复制到文档时,会重新定义文档中类似名称的样式以匹配模板中的样式描述。模板中的独特样式将复制到文档中。文档中的独特样式保持不变。 Below code 示例展示了如何将样式从一个文档复制到另一个文档中。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
string fileName = dataDir + "template.docx";
Document doc = new Document(fileName);
// Open the document.
Document target = new Document(dataDir + "TestFile.doc");
target.CopyStylesFromTemplate(doc);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dataDir);

如何操作主题属性

我们在 Aspose.Words 中添加了基本的 API 来访问文档主题属性。目前,该 API 包含以下公共对象:

  • 主题
  • 主题字体
  • 主题颜色

以下是获取主题属性的方法:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir);
Theme theme = doc.Theme;
// Major (Headings) font for Latin characters.
Console.WriteLine(theme.MajorFonts.Latin);
// Minor (Body) font for EastAsian characters.
Console.WriteLine(theme.MinorFonts.EastAsian);
// Color for theme color Accent 1.
Console.WriteLine(theme.Colors.Accent1);

以下是设置主题属性的方法:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir);
Theme theme = doc.Theme;
// Set Times New Roman font as Body theme font for Latin Character.
theme.MinorFonts.Latin = "Times New Roman";
// Set Color.Gold for theme color Hyperlink.
theme.Colors.Hyperlink = Color.Gold;