aspose.words 通过section获取当前节点的标题
时间: 2023-07-12 15:44:24 浏览: 75
要通过节(Section)获取当前节点的标题,您可以使用Aspose.Words API中的Section类来获取当前节点所在的节,然后使用该节的Header或Footer属性来获取标题。
以下是通过节获取当前节点标题的C#代码示例:
```
Document doc = new Document("input.docx");
Node currentNode = doc.CurrentNode;
if (currentNode != null && currentNode.NodeType == NodeType.Paragraph)
{
Paragraph currentParagraph = (Paragraph)currentNode;
// Get the section that contains the current node
Section section = currentParagraph.GetAncestor<Node>(NodeType.Section) as Section;
if (section != null)
{
// Get the header of the section
HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary];
if (header != null)
{
// Output the text of the header (assuming it contains a title)
Console.WriteLine("The header of the current section is: " + header.GetText());
}
else
{
Console.WriteLine("No header found for the current section.");
}
}
else
{
Console.WriteLine("The current node is not in a section.");
}
}
```
这个示例会输出包含当前节点的节的标题。请注意,我们使用Paragraph.GetAncestor方法来获取当前节点的祖先节,然后使用Section.HeadersFooters属性和HeaderFooterType.HeaderPrimary枚举值来获取节的主标题。如果找不到标题,则会输出相应的消息。