We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Hyperlinking .DOCX

Hi,

I have tried this code for hyperlinking occurrences of a particular string but I am getting duplicate values of the string appended with the original string in the output document. for example : I want to hyperlink 800 f2d 111 in the document but if the input document is having 800 f2d 111, then m getting an output 800 f2d 111800 f2d 111f2d 800 f2d 111111 instead of 800 f2d 111 .Please help me understand how the code is working and why am I getting the particular output.The java code is :

public class FindAndReplace {
static Document doc=null;
public static Document getDoc() {
return doc;
}
public static void setDoc(Document doc) {
FindAndReplace.doc = doc;
}
public static void main(String[] args) throws Exception
{
// Sample infrastructure.
URI exeDir = FindAndReplace.class.getResource("").toURI();
//System.out.println(exeDir.toString());
File dataDir = new File(“C:\Users\LAKHOTRX\DCW\AsposePOC\src\TestDoc.docx”);
System.out.println(dataDir.getAbsolutePath());
doc = new Document(dataDir.getAbsolutePath());

// We want the “your document” phrase to be highlighted.
Pattern regex = Pattern.compile(“800 f2d 111”, Pattern.CASE_INSENSITIVE);
// Generally it is recommend if you are modifying the document in a custom replacement evaluator
// then you should use backward replacement by specifying false value to the third parameter of the replace method.
doc.getRange().replace(regex, new ReplaceEvaluatorFindAndHighlight(), false);

// Save the output document.
doc.save(dataDir + “TestFileOut.doc”);
}
}

class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback
{
/
* This method is called by the Aspose.Words find and replace engine for each match.
* This method highlights the match string, even if it spans multiple runs.
*/
public int replacing(ReplacingArgs e) throws Exception
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run)currentNode, e.getMatchOffset());

// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength))
{
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
splitRun((Run)currentNode, remainingLength);
runs.add(currentNode);
}
// // Move to mathed node

// Now highlight all runs in the sequence.
for (Run run : (Iterable) runs)
{ DocumentBuilder builder = new DocumentBuilder(FindAndReplace.getDoc());
// run.getFont().setHighlightColor(Color.YELLOW);
builder.moveTo(run);
builder.getFont().setUnderline(Underline.SINGLE);
builder.getFont().setColor(Color.BLUE);
builder.insertHyperlink(e.getMatch().group(), “http://www.google.com”,
false);
}
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.REPLACE;
}

/
* Splits text of the specified run into two runs.
* Inserts the new run just after the specified run.
*/
private static Run splitRun(Run run, int position) throws Exception
{
Run afterRun = (Run)run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}

Regards,
Rajesh

Hi there,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Hi,

PFA

Hi,

Thanks for the help… now I can hyperlink the required part but I also need to reserve the same format. Is there any way in which I can do the same??

Regards,
Rajesh

Hi there,

Thanks for your inquiry. The font formatting will be same as the Run node to which you move the cursor and insert the hyperlink. Could you please share the expected output document? We will then provide you more information on this along with code.

Hi,

PFA . There should be a logic to check if the particular word being searched for is hyperlinked at its previous occurrence. If yes, then move to the next occurrence and hyperlink only that occurrence and stop. The output file attached shows how the desired output should be.


Regards,
Rajesh

Hi,


I am not getting the desired output as was requested in the output document in the previous post.

Regards,
Rajesh

Hi Rajesh,

Thanks for your inquiry. I have used the following code example to test this scenario again and have not found any issue in output document. I have attached the output document with this post for your kind reference. I have changed the hyperlink from http://www.google.com to http://www.aspose.com in FindandInsertHyperlink class.

If you are using different input document, please share your input, output and expected output documents here for investigation purposes. I will investigate the issue on my side and provide you more information.

Document doc = new Document(MyDir + "TestDocInput.docx");
Pattern regex = Pattern.compile("800f2d111", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, new FindandInsertHyperlink("800f2d111"), false);
doc.save(MyDir + "Out.docx");

Hi,

I have downloaded the latest jar 15.7.0 and tried running the code with that but still not getting the desired output.Please check with the input file once more.

Hi Rajesh,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.


  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi,

PFA.
And the texts should also preserve the format. If we pass a string as parameter to the constructor,then the output loses its formatting.

Regards,
Rajesh

Hi Rajesh,

Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v15.7.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v15.7.0 and let us know how it goes on your side. I hope, this will help. I have attached the output
document with this post for your kind reference.

Hi,

I am already using the latest version 15.7.0. Could you please try with the search string as “800 f2d 111” and check if the formatting is preserved.

Regards,
Rajesh