简体   繁体   English

如何使用 Aspose Words for Androd 获取内联词的超链接边界?

[英]How to get hyperlink boundaries of inline words with Aspose Words for Androd?

The android app reading paragraphs and some properties in Ms Word document with Aspose Words for Android library. android 应用程序使用 Aspose Words for Android 库读取 Ms Word 文档中的段落和一些属性。 It's getting paragraph text, style name and is seperated value.它获取段落文本、样式名称和单独的值。 There are some words have hyperlink in paragraph line.有一些单词在段落行中有超链接。 How to get start and end boundaries of the hyperlink of words?如何获取单词超链接的开始和结束边界? For example:例如:

This is an inline hyperlink paragraph example that the start bound is 18 and end bound is 27.这是一个内联超链接段落示例,起始界限为 18,结束界限为 27。

public static ArrayList<String[]> GetBookLinesByTag(String file) {

    ArrayList<String[]> bookLines = new ArrayList<>();

    try {
        Document doc = new Document(file);
        ParagraphCollection paras = doc.getFirstSection().getBody().getParagraphs();
        for(int i = 0; i < paras.getCount(); i++){
            String styleName = paras.get(i).getParagraphFormat().getStyleName().trim();
            String isStyleSeparator = Integer.toString(paras.get(i).getBreakIsStyleSeparator() ? 1 : 0);
            String content = paras.get(i).toString(SaveFormat.TEXT).trim();
            bookLines.add(new String[]{content, styleName, isStyleSeparator});
        }
    } catch (Exception e){}

    return bookLines;
}

Edit: Thanks Alexey Noskov , solved with you.编辑:谢谢Alexey Noskov ,和你一起解决。

public static ArrayList<String[]> GetBookLinesByTag(String file) {

    ArrayList<String[]> bookLines = new ArrayList<>();

    try {
        Document doc = new Document(file);
        ParagraphCollection paras = doc.getFirstSection().getBody().getParagraphs();
        for(int i = 0; i < paras.getCount(); i++){
            String styleName = paras.get(i).getParagraphFormat().getStyleName().trim();
            String isStyleSeparator = Integer.toString(paras.get(i).getBreakIsStyleSeparator() ? 1 : 0);
            String content = paras.get(i).toString(SaveFormat.TEXT).trim();

            for (Field field : paras.get(i).getRange().getFields()) {
                if (field.getType() == FieldType.FIELD_HYPERLINK) {
                    FieldHyperlink hyperlink = (FieldHyperlink) field;
                    String urlId = hyperlink.getSubAddress();
                    String urlText = hyperlink.getResult();
                    // Reformat linked text: urlText:urlId 
                    content = urlText + ":" + urlId;
                }
            }

            bookLines.add(new String[]{content, styleName, isStyleSeparator});
        }

    } catch (Exception e){}

    return bookLines;
}

Hyperlinks in MS Word documents are represented as fields. MS Word 文档中的超链接表示为字段。 If you press Alt+F9 in MS Word you will see something like this如果您在 MS Word 中按 Alt+F9,您将看到类似这样的内容

{ HYPERLINK "https://aspose.com" }

Follow the link to learn more about fields in Aspose.Words document model and in MS Word.点击链接了解有关 Aspose.Words 文档 model 和 MS Word 中的字段的更多信息。 https://docs.aspose.com/display/wordsjava/Introduction+to+Fields https://docs.aspose.com/display/wordsjava/Introduction+to+Fields

In your case you need to locate position of FieldStart – this will be the start position, then measure length of content between FieldSeparator and FieldEnd – start position plus the calculated length will the end position. In your case you need to locate position of FieldStart – this will be the start position, then measure length of content between FieldSeparator and FieldEnd – start position plus the calculated length will the end position.

Disclosure: I work at Aspose.Words team.披露:我在 Aspose.Words 团队工作。

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

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