Aspose.Words for .NET样式处理教程——如何根据样式提取内容

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入关于“样式处理”的介绍,在Aspose.Words中学会如何根据样式提取内容。


如何根据样式提取内容

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

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

使用Aspose.Words检索Word文档中以特定样式设置格式的文本的解决方案通常是经济且直接的。为了说明Aspose.Words如何轻松处理基于样式的内容,让我们看一个示例。在此示例中,我们将从示例Word文档中检索具有特定段落样式和字符样式格式的文本,这将涉及以下内容:

  • 使用Document类打开Word文档。文档中的所有段落和所有运行。
  • 仅选择所需的段落和运行。

具体来说,将从此示例Word文档中检索以“标题1”段落样式和“强烈强调”字符样式设置格式的文本。在此示例中,使用“标题1”段落样式设置格式的文本是“插入标签”,“快速样式”和“主题”,使用“强烈强调”字体设置的文本是蓝色的几种实例,斜体,粗体文本,例如“画廊”和“整体外观”。

在Aspose中,基于样式的查询的实现非常简单。word文档对象模型,因为它只是使用了已经存在的工具。这个解决方案实现了两个类方法:

  • hsbystylename——这个方法检索文档中具有特定样式名称的段落的数组。
  • RunsByStyleName—此方法检索文档中具有特定样式名称的运行的数组。

这两种方法非常相似,唯一的区别是节点类型和段落和运行节点中样式信息的表示形式。下面是段落bystylename的一个实现:在下面的例子中找到所有使用指定格式的段落。

// 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.ParagraphFormat 对象的Style.Name 属性中找到。 尽管我们使用NodeType.Run 检索运行节点,但RunsByStyleName的实现几乎相同。Run 对象的Font.Style 属性用于访问运行节点。下面的示例查找所有以指定样式设置格式的运行。

// 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);

最终结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值