aspose.words 获取当前节点的二级标题
时间: 2023-07-12 19:44:24 浏览: 57
要获取当前节点的二级标题,您可以使用Aspose.Words API中的Range类来查找当前节点之前的所有标题,并筛选出标题级别为2的标题。
以下是获取当前节点之前的二级标题文本的C#代码示例:
```
Document doc = new Document("input.docx");
Node currentNode = doc.CurrentNode;
if (currentNode != null && currentNode.NodeType == NodeType.Paragraph)
{
Paragraph currentParagraph = (Paragraph)currentNode;
// Find all paragraphs before the current paragraph with heading level 2
NodeCollection headings = doc.GetChildNodes(NodeType.Paragraph, true);
List<string> secondLevelHeadings = new List<string>();
foreach (Paragraph heading in headings)
{
if (heading.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2 &&
heading.Range.EndOffset < currentParagraph.Range.StartOffset)
{
secondLevelHeadings.Add(heading.GetText());
}
}
// Output the text of the last second-level heading found
if (secondLevelHeadings.Count > 0)
{
Console.WriteLine("The most recent second-level heading is: " + secondLevelHeadings[secondLevelHeadings.Count - 1]);
}
else
{
Console.WriteLine("No second-level headings found before the current node.");
}
}
```
这个示例会输出当前节点之前的所有二级标题中最新的一个。请注意,我们使用ParagraphFormat.StyleIdentifier属性来确定段落的样式,然后筛选出标题级别为2的段落,并且只考虑在当前段落之前的标题。