Document Solutions for Word
Features / Shapes / Online Video
In This Topic
    Online Video
    In This Topic

    DsWord allows you to insert and play online videos directly from the document without leaving the document. You can add an online video to the document by inserting the URL of the video into the picture created with Picture class using SetUrl method of WebVideoProperties class.

    SetUrl method has three overloads, allowing you to specify the input URL, title, height, and width of the video. This method generates the appropriate EmbeddedHtml property string internally, providing a convenient way for you to define these parameters that may be challenging to write directly.

    Note: EmbeddedHtml property will receive width and height either from parameters or directly from Width and Height properties of WebVideoProperties class when you use an overload without them. EmbeddedHtml, Width, and Height properties are serialized as distinct OpenXML tags.

    Refer to the following example code to insert the URL of the video to add online video to the document:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var doc = new GcWordDocument();
    
    // Add picture to document.
    var image = doc.Body.Paragraphs.Add().AddRun().AddPicture();
                
    // Set poster frame image.
    byte[] imageBytes1 = File.ReadAllBytes("sunrise.jpg");
    image.ImageData.SetImage(imageBytes1, contentType: "image/jpg");
    
    // Add online video Url and set title, width, and height.
    image.ImageData.WebVideoProperties.SetUrl("https://www.youtube.com/watch?v=gsnqXt7d1mU&list=PL4Gr5tOAPttLOY9IrWVjJlv4CtkYI5cI_&index=2", "Sunrise", 600, 300);
    
    // Save the document.
    doc.Save("OnlineVideo.docx");

    Customize Hyperlink Action

    DsWord provides HyperlinkOnClick and HyperlinkOnHover properties of Shape classes such as ShapePictureGroupShapeCanvasShape, and InkShape, which are derived from ShapeBase, that enable you to customize or redefine the click and hover action of the hyperlink.

    You can also add and define hyperlinks to other shapes using the properties of HyperlinkOnAction class. For more information, see Shape Hyperlink.

    Refer to the following example code to redefine click and hover actions:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var doc = new GcWordDocument();
    
    // Add picture to document.
    var image = doc.Body.Paragraphs.Add().AddRun().AddPicture();
    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");
    
    // Add online video Url.
    image.ImageData.WebVideoProperties.SetUrl("https://www.youtube.com/watch?v=gsnqXt7d1mU&list=PL4Gr5tOAPttLOY9IrWVjJlv4CtkYI5cI_&index=2", "Sunrise");
    
    // Set new address for the hyperlink.
    var newUrl = new Uri("https://www.youtube.com/watch?v=5p_SuO96Jd4&pp=ygUHc3VucmlzZQ%3D%3D", UriKind.RelativeOrAbsolute);
    image.HyperlinkOnClick.Address = newUrl;
    
    // Set new screen tip.
    image.HyperlinkOnHover.ScreenTip = "screen tip";
    
    // Save the document.
    doc.Save("RedefinedHyperlink.docx");

    Limitations

    Microsoft Word ignores the parameters of SetUrl method (width, height, and title) and Width and Height properties. The effects of these parameters and properties may be visible in alternative applications that also work with Word files.