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

Free Support Forum - aspose.com

node.getPreviousSibling() method doesn't traverse till top node

while traversing in reverse order using node.getPreviousSibling() method from a node, it doesn’t traverse all the way till beginning of document and it return null at some intermediate node.

build environment - Java 1.8, aspose-words - 20.8

code snippet :

 public void findSignatureNode(String wordFileLocation) {
        try {
            Document doc = new Document(wordFileLocation);
            NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
            Node introductionNode = null;
            for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
                if (StringUtils.isNotEmpty(paragraph.getText()) && paragraph.getText().trim().equalsIgnoreCase("Introduction")) {
                    introductionNode = paragraph;
                    break;
                }
            }
            if (null != introductionNode) {
                Node previous = introductionNode;
                while (previous != null) {
                    if (StringUtils.isNotEmpty(previous.getText()) && previous.getText().trim().equalsIgnoreCase("Signature Page")) {
                        System.out.println("-- found -- Signature Page Node");
                    }
                    if (StringUtils.isNotEmpty(previous.getText()) && previous.getText().trim().equalsIgnoreCase("Revision History")) {
                        System.out.println("-- found -- Revision History Node");
                    }
                    previous = previous.getPreviousSibling();
                }
            } else System.out.println("Introduction node is null");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

File used : issue1_signature_page.docx (54.9 KB)

Expected Output :

-- found -- Revision History Node
-- found -- Signature Page Node

Actual Output:

-- found -- Revision History Node

@hkcw3 The reason for the problem is the same as here. You can take Alexey’s advice or consider using previousPreOrder(doc) instead of previousSibling(). However, you will have to slightly edit your algorithm in this case.