GroupDocs.Assembly for Java enables you to insert hyperlinks dynamically into documents using link tags. Hyperlinks can point to external URLs, bookmarks within the document, or specific cells/slides depending on the document type.
Here are the steps to insert hyperlinks dynamically:
Create a template document with link tags
Use the syntax <<link [uri_expression][display_text_expression]>>
Provide URI and optional display text from your data source
Assemble the document
Note
Link tags are replaced with clickable hyperlinks during document assembly. The URI expression is mandatory and must return a non-empty value, while display text is optional.
Insert Hyperlink with URL and Text
Insert a hyperlink with both URL and display text:
In your template (HyperlinkTemplate.docx), use link tags:
Please <<link [website.getUrl()][website.getName()]>> for more information.
After assembly:
Please Visit our website for more information.
(The text “Visit our website” will be a clickable hyperlink.)
Hyperlink with URL Only
If you omit the display text, the URL is used as the display text:
importcom.groupdocs.assembly.*;publicstaticvoidinsertHyperlinkUrlOnly()throwsException{Websitewebsite=newWebsite(null,"https://www.example.com");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("HyperlinkTemplate.docx","HyperlinkOutput.docx",newDataSourceInfo(website,"website"));System.out.println("Hyperlink with URL only inserted successfully.");}
Template:
Visit <<link [website.getUrl()]>> for more information.
Insert Multiple Hyperlinks from Collection
Insert hyperlinks for each item in a collection:
importcom.groupdocs.assembly.*;publicstaticvoidinsertHyperlinksFromCollection()throwsException{// Pass an array (or a POJO with a typed List<Website> getter) so that the engine
// knows the item type; a raw java.util.List passed directly loses its element type
Website[]websites=newWebsite[]{newWebsite("Home","https://www.example.com"),newWebsite("About","https://www.example.com/about"),newWebsite("Contact","https://www.example.com/contact")};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("HyperlinksTemplate.docx","HyperlinksOutput.docx",newDataSourceInfo(websites,"websites"));System.out.println("Hyperlinks inserted from collection successfully.");}
The same link can also be declared with constant values: <<link ["A1"]["Go to Home"]>>.
Hyperlink to Slide (Presentation Documents)
Link to slides in presentation documents:
importcom.groupdocs.assembly.*;publicclassSlideLinkData{privateStringslideNumber;privateStringdisplayText;publicSlideLinkData(StringslideNumber,StringdisplayText){this.slideNumber=slideNumber;this.displayText=displayText;}publicStringgetSlideNumber(){returnslideNumber;}publicStringgetDisplayText(){returndisplayText;}}publicstaticvoidinsertSlideLink()throwsException{SlideLinkDatadata=newSlideLinkData("Slide1","Go to First Slide");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("SlideLinkTemplate.pptx","SlideLinkOutput.pptx",newDataSourceInfo(data,"data"));System.out.println("Slide hyperlink inserted successfully.");}
The same link can also be declared with constant values: <<link ["Slide1"]["Go to First Slide"]>>.
Using URI Objects
You can use java.net.URI (or java.net.URL) objects directly:
importcom.groupdocs.assembly.*;importjava.net.URI;publicclassWebsiteWithUri{privateURIurl;privateStringname;publicWebsiteWithUri(URIurl,Stringname){this.url=url;this.name=name;}publicURIgetUrl(){returnurl;}publicStringgetName(){returnname;}}publicstaticvoidinsertHyperlinkWithUri()throwsException{WebsiteWithUriwebsite=newWebsiteWithUri(newURI("https://www.example.com"),"Visit our website");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("HyperlinkTemplate.docx","HyperlinkOutput.docx",newDataSourceInfo(website,"website"));System.out.println("Hyperlink with URI object inserted successfully.");}
Warning
Link tags cannot be used within charts. For hyperlinks in charts, use alternative approaches or place links outside chart elements.
Advanced Usage Topics
To learn more about bookmark creation, complex hyperlink scenarios, and advanced link formatting, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: