Document Solutions for Excel, Java Edition | Document Solutions
Features / Shapes And Pictures / Hyperlink on Shape
In This Topic
    Hyperlink on Shape
    In This Topic

    In DsExcel, hyperlinks can be added to various shape types like basic shapes, charts, connectors, pictures and group shapes. It allows users to quickly navigate to related information on a webpage, external file, specific range in the same workbook, or email address by clicking on the shape.

    Note: Hyperlink cannot be added to Comment and Slicer shape types.

    Hyperlinks can be configured using the following properties of the IHyperlink interface.

    1. The setAddress and setSubAddress methods of the IHyperlink interface can be used to configure the hyperlink references. The table shown below illustrates both the properties with examples:
      Link To Address SubAddress
      External File Example: "C:\Users\Desktop\test.xlsx" null
      Webpage Example: "https://developer.mescius.com/" null
      A range in this document Example: null "Sheet1!$C$3:$E$4"
      Email Address Example: "mailto: abc.xyz@mescius.com" null

    2. The setEmailSubject method can be used to set the text of hyperlink's email subject line.
    3. The setScreenTip method can be used to set the tip text for the specified hyperlink.
    4. The setTextToDisplay method can be used to set the text to be displayed for the specified hyperlink.

    Add Hyperlink

    A user can add hyperlink to a shape in a worksheet using the Add method of the IHyperLinks interface.

    Refer to the following example code to insert hyperlinks on shapes to redirect to an external file, webpage, range within the worksheet and email address.

    Java
    Copy Code
    // Add a hyperlink to external file
            
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    // Add a Shape
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 1, 1, 200, 100);
    shape.getTextFrame().getTextRange().getParagraphs().add("Link to Test.xlsx file");
    // Add Hyperlink
    worksheet.getHyperlinks().add(shape, "C:\\Test.xlsx", null, "Link to Test.xlsx file", "Test.xlsx");
    
    // Save to an excel file
    workbook.save("402-LinkExternalFile.xlsx", SaveFileFormat.Xlsx);
    Java
    Copy Code
    // Add a hyperlink to web page
            
    // Add a Shape
    IShape picture = null;
    try {
        picture = worksheet.getShapes().addPicture("logo.jpg", 1, 1, 100, 100);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Add Hyperlink
    worksheet.getHyperlinks().add(picture, "https://developer.mescius.com/", null, "Click to Open", "MESCIUS, Inc.");
    // Save to an excel file
    workbook.save("401-ShapeHyperlink.xlsx", SaveFileFormat.Xlsx);
    Java
    Copy Code
    // Add a Shape
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 1, 1, 200, 100);
    shape.getTextFrame().getTextRange().getParagraphs().add("Go To sheet1 J3:K4");
    // Add Hyperlink
    worksheet.getHyperlinks().add(shape, null, "Sheet1!J3:K4", "Go To Sheet1 D3:E4", null);
    
    // Save to an excel file
    workbook.save("403-HyperlinkRange.xlsx", SaveFileFormat.Xlsx);
    Java
    Copy Code
    //Add a hyperlink to email address.
            
    // Add a Shape
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 1, 1, 200, 100);
    shape.getTextFrame().getTextRange().getParagraphs().add("Send Feedback");
    // Add Hyperlink
    worksheet.getHyperlinks().add(shape, "mailto:web_feedback@mescius.com", null, "Send your valuable feedback.",
            "Feedback");
    
    // Save to an excel file
    workbook.save("404-HyperlinkMailTo.xlsx", SaveFileFormat.Xlsx);

    Delete Hyperlink

    The hyperlink on the shape can be removed using the Delete method of the IHyperlink interface.

    Refer to the following example code to delete hyperlink.

    Java
    Copy Code
    //Delete a hyperlink.
            
    // Add a Shape
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 1, 1, 200, 100);
    
    // Create Hyperlinks
    IHyperlink hyperlink1 = worksheet.getHyperlinks().add(shape, "https://developer.mescius.com/", null, "Click to Open",
            "MESCIUS, Inc.");
    
    // Delete hyperlink1.
    hyperlink1.delete();
    
    // Save to an excel file
    workbook.save("405-DeleteHyperlink.xlsx", SaveFileFormat.Xlsx);