在 Aspose Words for Android 中获取段落中字符样式文本的起始位置

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

下面的代码已经用 Aspose Words for Android 解析了 Ms Word 文档。文档中的所有段落都有单独的内联字符样式文本。我有它们的文本和样式,但有什么方法可以在其段落字符串中获取它们的起始位置,例如 String.indexOf() ?可能会转成字符串,但这种情况下无法进行样式控制。

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; // ?
            }
        }

}

您可以在样式 运行 之前的 运行 秒内计算文本长度。像这样。

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();
    }
}