javaandroidparsingasposeaspose.words

Get start position of character styled texts in a paragraph in Aspose Words for Android


It has being parsed Ms Word documents with Aspose Words for Android below code. All of paragraphs in the document have inline character styled texts separately. I've text and style of them but are there any way to get start position of them in its paragraph string like String.indexOf() ? It may be convert to string, but style control is not possible in this case.

Document doc = new Document(file); // Get word document.
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.

for (Paragraph prg : (Iterable<Paragraph>) paras) {

    for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){

            boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
            
            // Get different styled texts only.
            if (!defaultPrgFont){
                // Text in different styled according to paragraph.
                String runText = run.getText();
                // Style of the different styled text.
                String runStyle = run.getFont().getStyle().getName()
                // Start position of the different styled text in its paragraph. 
                int runStartPosition; // ?
            }
        }

}

Solution

  • You can calculate length of text in runs before the styled run. Something like this.

    Document doc = new Document("C:\\Temp\\in.docx"); // Get word document.
    NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true); // get all paragraphs.
    
    for (Paragraph prg : (Iterable<Paragraph>) paras) {
    
        int runStartPosition = 0;
        for (Run run : (Iterable<Run>) prg.getChildNodes(NodeType.RUN, true)){
    
            boolean defaultPrgFont = run.getFont().getStyle().getName().equals("Default Paragraph Font");
    
            // Get different styled texts only.
            if (!defaultPrgFont){
                // Text in different styled according to paragraph.
                String runText = run.getText();
                // Style of the different styled text.
                String runStyle = run.getFont().getStyle().getName();
    
                System.out.println(runStartPosition);
            }
    
            // Position is increased for all runs in the paragraph.
            // Note that some runs might represent field codes and are not normally displayed.
            runStartPosition += run.getText().length();
        }
    }