We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get the style associated with 'any' nodetype

Hi there,
I’m traversing the nodes in the document. With a particular node in hand, need to know the style associated with it. Say if the node is table, need to know the table style associated with this.

Some of the distinguished way we can have:
for paragraph, ((Paragraph)node).getParagraphFormat().getStyle()
or for run ((Run)node).getFont().getStyle(),
or for table, ((Table)node).getStyle().

but we want some clean api for getting the style associated with the node. Something like Node.getStyle(). If node is not having style associated with it, then it could be null. That’s not a problem. Just curious if Aspose has some easy way for getting the style associated with the node.

Attaching the chat transcript, concerning discussion about this.

Hi Praneeth,

Thanks for your inquiry. I think, you can achieve your requirement by using following code example. Hope this helps you.


public Style getNodeFont(Node
node)
throws Exception

{

if(node.getNodeType() == NodeType.RUN)

{

return ((Run)node).getFont().getStyle();

}

else if(node.getNodeType() == NodeType.PARAGRAPH)

{

return ((Paragraph)node).getParagraphFormat().getStyle();

}

else if(node.getNodeType() == NodeType.TABLE)

{

return ((Table)node).getStyle();

}

else if(node.getNodeType() == NodeType.FIELD_START)

{

return ((FieldStart)node).getFont().getStyle();

}

else if(node.getNodeType() == NodeType.FIELD_END)

{

return ((FieldEnd)node).getFont().getStyle();

}

else if(node.getNodeType() == NodeType.STRUCTURED_DOCUMENT_TAG)

{

return ((StructuredDocumentTag)node).getContentsFont().getStyle();

}

else if(node.getNodeType() == NodeType.SHAPE)

{

return ((Shape)node).getFont().getStyle();

}

return null;

}