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

Free Support Forum - aspose.com

Get previous Heading?


How can I get, from the current documentBuilder position the previous heading in the document?

I was trying to get it from “documentBuilder.getCurrentParagraph().getPreviousSibling()” but now I don’t know how to check if the previous sibling is a heading or not.

thanks in advance


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, you should check node type, and if the current node is Paragraph, then you can check whether it is heading paragraph. Please see the following code for example.

Document doc = new Document("C:\\Temp\\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

// Move to the end of the document.

// We will search for heading paragraphs from the end.


Node currentNode = builder.getCurrentParagraph();

while (currentNode != null)


if (currentNode.getNodeType() == NodeType.PARAGRAPH && ((Paragraph)currentNode).getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1)


currentNode = currentNode.previousPreOrder(doc);


// Move DocumentBuilder cursor to the heading paragraph and insert some text.


builder.write("This text inserted by DocumentBuilder");

// Save output document.


Please let me know if you need more assistance, I will be glad to help you.

Best regards.