HyperlinkAction Class
Contains methods that create hyperlink actions for shapes.
Declaration
public class HyperlinkAction extends JavaObject
Remarks
A hyperlink performs an action when a user clicks a shape in a slide show. Call the following static HyperlinkAction methods to create actions:
| Action | Methods |
|---|---|
| Open a web page, a file, or an email message. | createUri(String uri), createUri(String uri, String targetFrame), createEmail(String address), createEmail(String address, String subject) |
| Move to the specified slide. | createSlideJump(Slide slide) |
| Move to the next, previous, first, last, or last viewed slide, or end the slide show. | getNextSlide(), getPreviousSlide(), getFirstSlide(), getLastSlide(), getLastViewedSlide(), getEndShow() |
To add a hyperlink to a shape, pass the action to the OfficeHyperlink(action) constructor and pass the created hyperlink to the ShapeBase.setHyperlink(value) method.
Export Hyperlinks to PDF
When you export a presentation to PDF, the API converts hyperlinks to PDF links as follows:
- URI actions become links if the URI uses the
http,https, ormailtoscheme and passes the API’s safety checks. The API skips other URIs (for example, file paths). - Slide actions become links to PDF pages. If the API does not export the target slide (for example, a hidden slide), the PDF document contains no link.
- Next, previous, first, and last slide actions become links to PDF pages. The API skips last viewed slide and end show actions.
Example
Add Hyperlinks to Shapes on a Slide
The following code snippet adds shapes with hyperlinks to a slide. The hyperlinks open a web page, create an email message, move to another slide, and end the slide show:
import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
public class Main {
public static void main(String[] args) throws Exception {
try (Presentation presentation = new Presentation()) {
presentation.getSlides().clear();
Slide menuSlide = new Slide(SlideLayoutType.BLANK);
Slide summarySlide = new Slide(SlideLayoutType.BLANK);
presentation.getSlides().add(menuSlide);
presentation.getSlides().add(summarySlide);
// Open a web page in a new browser window.
OfficeHyperlink webLink = new OfficeHyperlink(HyperlinkAction.createUri("https://www.devexpress.com", HyperlinkTargetFrame.BLANK));
webLink.setToolTip("Visit the DevExpress website");
addLinkedShape(menuSlide, "DevExpress Website", webLink, 100);
// Create an email message with the specified subject.
addLinkedShape(menuSlide, "Contact Us",
new OfficeHyperlink(HyperlinkAction.createEmail("info@example.com", "Product question")), 450);
// Move to the second slide.
addLinkedShape(menuSlide, "Summary",
new OfficeHyperlink(HyperlinkAction.createSlideJump(summarySlide)), 800);
// End the slide show.
addLinkedShape(menuSlide, "Exit", new OfficeHyperlink(HyperlinkAction.getEndShow()), 1150);
Files.createDirectories(Path.of("output"));
try (OutputStream stream = Files.newOutputStream(Path.of("output", "Document.pptx"))) {
presentation.save(stream);
}
}
}
static void addLinkedShape(Slide slide, String text, OfficeHyperlink hyperlink, float y) {
Shape shape = new Shape(ShapeType.getRectangle(), 100, y, 1200, 250);
shape.setName(text);
shape.getTextArea().setText(text);
shape.setHyperlink(hyperlink);
slide.getShapes().add(shape);
}
}
Inherited Members
Inheritance
HyperlinkAction()
Initializes a new instance of the HyperlinkAction class.
Declaration
public HyperlinkAction()
Methods
createEmail(String address, String subject) Method
Creates an action that opens a new email message with the specified address and subject.
Declaration
public static OfficeHyperlinkAction createEmail(String address, String subject)
Parameters
| Name | Type | Description |
|---|---|---|
| address | String | The recipient email address. |
| subject | String | The email message subject. |
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The URI action. |
Exceptions
| Type | Description |
|---|---|
| ArgumentException | Thrown if the |
Remarks
The createEmail method creates a mailto URI action and encodes the subject (for example, mailto:info@example.com?subject=Product%20question). If the subject value is null or an empty string, the URI contains no subject.
createEmail(String address) Method
Creates an action that opens a new email message to the specified address.
Declaration
public static OfficeHyperlinkAction createEmail(String address)
Parameters
| Name | Type | Description |
|---|---|---|
| address | String | The recipient email address. |
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The URI action. |
Exceptions
| Type | Description |
|---|---|
| ArgumentException | Thrown if the |
Remarks
The createEmail method creates a mailto URI action (for example, mailto:info@example.com). Call the UriHyperlinkAction.getUri() method of the returned action to get the URI.
createSlideJump(Slide slide) Method
Creates an action that moves to the specified slide.
Declaration
public static OfficeHyperlinkAction createSlideJump(Slide slide)
Parameters
| Name | Type | Description |
|---|---|---|
| slide | Slide | The target slide. |
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide action. |
Exceptions
| Type | Description |
|---|---|
| ArgumentNullException | Thrown if the |
Remarks
Pass a slide from the presentation that contains the shape. If the presentation does not contain the target slide when you save the presentation, the API does not save the hyperlink.
createUri(String uri, String targetFrame) Method
Creates an action that opens the specified URI in the specified frame.
Declaration
public static OfficeHyperlinkAction createUri(String uri, String targetFrame)
Parameters
| Name | Type | Description |
|---|---|---|
| uri | String | The URI that the hyperlink opens (for example, a web page address). |
| targetFrame | String | The name of the frame or window in which the URI opens (for example, |
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The URI action. |
Exceptions
| Type | Description |
|---|---|
| ArgumentException | Thrown if the |
Remarks
To open the URI in a predefined frame (for example, a new window), pass a HyperlinkTargetFrame field to the createUri method. You can also pass a custom frame name.
If the targetFrame value is null or an empty string, the action does not specify a target frame.
createUri(String uri) Method
Creates an action that opens the specified URI.
Declaration
public static OfficeHyperlinkAction createUri(String uri)
Parameters
| Name | Type | Description |
|---|---|---|
| uri | String | The URI that the hyperlink opens (for example, a web page address). |
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The URI action. |
Exceptions
| Type | Description |
|---|---|
| ArgumentException | Thrown if the |
getEndShow() Method
Returns an action that ends the slide show.
Declaration
public static OfficeHyperlinkAction getEndShow()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |
getFirstSlide() Method
Returns an action that moves to the first slide.
Declaration
public static OfficeHyperlinkAction getFirstSlide()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |
getLastSlide() Method
Returns an action that moves to the last slide.
Declaration
public static OfficeHyperlinkAction getLastSlide()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |
getLastViewedSlide() Method
Returns an action that moves to the last viewed slide.
Declaration
public static OfficeHyperlinkAction getLastViewedSlide()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |
getNextSlide() Method
Returns an action that moves to the next slide.
Declaration
public static OfficeHyperlinkAction getNextSlide()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |
getPreviousSlide() Method
Returns an action that moves to the previous slide.
Declaration
public static OfficeHyperlinkAction getPreviousSlide()
Returns
| Type | Description |
|---|---|
| OfficeHyperlinkAction | The slide show action. |