简体   繁体   中英

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. 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.

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.

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. If you press Alt+F9 in MS Word you will see something like this

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

Follow the link to learn more about fields in Aspose.Words document model and in MS Word. 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.

Disclosure: I work at Aspose.Words team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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