HyperLink Information

Is there any way to detect a run has hyperlink contents or not? or is there a way to get only the the url of that hyperlink from the run?

Hi Abhradeep,


Thanks for your inquiry. The following code iterates through all the Run nodes in document and determines if a Run belongs to a HYPERLINK field or not.
Document doc = new Document(getMyDir() + “in.docx”);

NodeCollection runNodes = doc.getChildNodes(NodeType.RUN, true);
for (Run run : (Iterable<Run>) runNodes)
{
for (Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_HYPERLINK) {
Node currentNode = field.getStart();
boolean isInside = false;
while (currentNode != field.getEnd() && !isInside) {
if (currentNode.getNodeType() == NodeType.RUN)
if (currentNode.equals(run))
isInside = true;

            Node nextNode <font color="BLUE">=</font> currentNode<font color="BLUE"><b>.</b></font>nextPreOrder<font color="BLUE"><b>(</b></font>currentNode<font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
            currentNode <font color="BLUE">=</font> nextNode<font color="BLUE"><b>;</b></font>
        <font color="BLUE"><b>}</b></font>
        System<font color="BLUE"><b>.</b></font>out<font color="BLUE"><b>.</b></font>println<font color="BLUE"><b>(</b></font>isInside<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

}

I hope, this helps.

Moreover, please refer to the Hyperlink class in the following article which might be helpful to you:
http://www.aspose.com/docs/display/wordsjava/How+to++Replace+or+Modify+Hyperlinks

Best regards,