【问题标题】:Get start position of character styled texts in a paragraph in Aspose Words for Android在 Aspose Words for Android 中获取段落中字符样式文本的起始位置
【发布时间】:2020-07-11 12:20:17
【问题描述】:

它已在下面的代码中使用 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; // ?
            }
        }

}

【问题讨论】:

    标签: java android parsing aspose aspose.words


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多