Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
A hyperlink in Microsoft Word documents is the HYPERLINK
field. In Aspose.Words, hyperlinks are implemented through the FieldHyperlink class.
Use the InsertHyperlink method to insert a hyperlink into the document. This method accepts three parameters:
URL
is a name of a bookmark inside a documentThe InsertHyperlink method always adds apostrophes at the beginning and end of the URL.
Font
property.
The following code example shows how to insert a hyperlink into a document using DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Please make sure to visit "); | |
builder.getFont().setColor(Color.BLUE); | |
builder.getFont().setUnderline(Underline.SINGLE); | |
builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); | |
builder.getFont().clearFormatting(); | |
builder.write(" for more information."); | |
doc.save(getArtifactsDir() + "AddContentUsingDocumentBuilder.InsertHyperlink.docx"); |
Hyperlink in Microsoft Word documents is a field. A field in a Word document, as we said earlier, is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document.
To replace or modify hyperlinks, it is need to find the hyperlinks in the document and replace either their text, URLs, or both.
The following code example shows how to find all hyperlinks in Word document and changes their URL
and display name:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(getMyDir() + "Hyperlinks.docx"); | |
for (Field field : doc.getRange().getFields()) | |
{ | |
if (field.getType() == FieldType.FIELD_HYPERLINK) | |
{ | |
FieldHyperlink hyperlink = (FieldHyperlink) field; | |
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these. | |
if (hyperlink.getSubAddress() != null) | |
continue; | |
hyperlink.setAddress("http://www.aspose.com"); | |
hyperlink.setResult("Aspose - The .NET & Java Component Publisher"); | |
} | |
} | |
doc.save(getArtifactsDir() + "WorkingWithFields.ReplaceHyperlinks.docx"); |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.