Получить начальную позицию текста со стилем символов в абзаце в Aspose Words для Android - PullRequest
2 голосов
/ 11 июля 2020

Были проанализированы документы Ms Word с помощью слов Aspose для кода Android ниже. Все абзацы в документе имеют текст, стилизованный под строчные символы. У меня есть их текст и стиль, но есть ли способ получить их начальную позицию в строке абзаца, например 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; // ?
            }
        }

}

1 Ответ

1 голос
/ 13 июля 2020

Вы можете рассчитать длину текста в прогонах до стилизованного прогона. Примерно так.

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();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...