Document Solutions for Word
Features / Shapes / Shape Hyperlink
In This Topic
    Shape Hyperlink
    In This Topic

    Providing hyperlinks in a shape makes it convenient to navigate to the required information. DsWord enables you to use or define hyperlink actions when hovering or clicking on the hyperlink using HyperlinkOnAction class and HyperlinkOnClick and HyperlinkOnHover properties of Shape classes such as ShapePictureGroupShapeCanvasShape, and InkShape, which are derived from ShapeBase.

    HyperlinkOnAction class provides AddressActionHighlightClickViewedTargetInvalidUrl, and ScreenTip properties that enable you to set various hyperlink properties when you hover over it or click on it. These properties also allow you to use the Edit Link, Open Link, and Remove Link options in Microsoft Word.

    Refer to the following example code to define HyperlinkOnAction and use it in the shape's hyperlink:

    C#
    Copy Code
    static void Main(string[] args)
    {
        // Initialize GcWordDocument.
        var doc = new GcWordDocument();
    
        // Add picture to document.
        var image = doc.Body.Paragraphs.Add().AddRun().AddPicture();
    
        // Set picture width and height.
        image.Size.Width.Value = 500f;
        image.Size.Height.Value = 500f;
    
        // Set poster frame image.
        byte[] imageBytes1 = File.ReadAllBytes("sunrise.jpg");
        image.ImageData.SetImage(imageBytes1, contentType: "image/jpg");
    
        // Set click and hover actions for hyperlink.
        SetHyperlinkOnAction(image.HyperlinkOnClick);
        SetHyperlinkOnAction(image.HyperlinkOnHover);
    
        // Save the document.
        doc.Save("ShapeHyperlink.docx");
    }
    
    // Define HyperlinkOnAction.
    static void SetHyperlinkOnAction(HyperlinkOnAction ha)
    {
        var url = "https://www.youtube.com/watch?v=gsnqXt7d1mU&list=PL4Gr5tOAPttLOY9IrWVjJlv4CtkYI5cI_&index=2";
        ha.Address = new Uri(url);
        ha.Action = "action";
        ha.HighlightClick = true;
        ha.Viewed = true;
        ha.Target = "target";
        ha.InvalidUrl = "invalid url";
        ha.ScreenTip = "screen tip";
    }

    Limitations