简体   繁体   English

在 Aspose Words for Android 的段落中开始 position 的字符样式文本

[英]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.它已在代码下方使用 Aspose Words for Android 解析 Ms Word 文档。 All of paragraphs in the document have inline character styled texts seperatelly.文档中的所有段落都具有单独的内联字符样式文本。 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()?我有它们的文本和样式,但是有什么方法可以在像 String.indexOf() 这样的段落字符串中开始它们的 position? 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; // ?
            }
        }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM