How to Embed Media File in Word in C#, VB.NET

2015-08-12 02:23:09 Written by  support iceblue
Rate this item
(0 votes)

Spire.Doc supports to insert any type of file such as Excel, PDF, PowerPoint and etc, as OLE object into a Word document. In this article, you'll learn how to add a media file (audio or video) to a Word document using Spire.Doc in C#, VB.NET.

In the class of DocOleObject, a method named AppendOleObject(Stream oleStream, DocPicture olePicture, string fileExtension) is available for users to insert media file with the extension of mp3, mp4, avi or any other format into a Word document. The three parameters in this method represent:

  • oleStream: The OLE file stream.
  • olePicture: The image (icon) that is displayed in Word to show the OLE object.
  • fileExtension: The file extension.

Code Snippet:

Step 1: Initialize a new instance of Document class and add a new section.

Document doc = new Document();
Section section = doc.AddSection();

Step 2: Add a new paragraph, append some formatted text into the paragraph.

Paragraph para1 = section.AddParagraph();
para1.AppendText("Double click the PLAY button to view the video file");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "Style";
style1.CharacterFormat.FontName = "Calibri";
style1.CharacterFormat.FontSize = 15;
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.TextColor = Color.Red;
doc.Styles.Add(style1);
para1.ApplyStyle(style1.Name);

Step 3: Add another paragraph, append a video file as OLE object into the paragraph.

Paragraph para2 = section.AddParagraph();
Stream s = File.OpenRead("media.mp4");
DocPicture pic = new DocPicture(doc);
pic.LoadImage(Image.FromFile("button.png"));
para2.AppendOleObject(s, pic, "mp4");

Step 4: Save the view the file.

doc.SaveToFile("Result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("Result.docx");

Output:

How to Embed Media File in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;
namespace EmbedMediaFile
{
    class Program
    {

        static void Main(string[] args)
        {
            //create a new Word document and insert section
            Document doc = new Document();
            Section section = doc.AddSection();
            //add a paragraph and append some text
            Paragraph para1 = section.AddParagraph();
            para1.AppendText("Double click the PLAY button to view the video file");
            ParagraphStyle style1 = new ParagraphStyle(doc);
            style1.Name = "Style";
            style1.CharacterFormat.FontName = "Calibri";
            style1.CharacterFormat.FontSize = 15;
            style1.CharacterFormat.Bold = true;
            style1.CharacterFormat.TextColor = Color.Red;
            doc.Styles.Add(style1);
            para1.ApplyStyle(style1.Name);
            //add another paragraph, append video file as OLE object in Word
            Paragraph para2 = section.AddParagraph();
            Stream s = File.OpenRead("media.mp4");
            DocPicture pic = new DocPicture(doc);
            pic.LoadImage(Image.FromFile("button.png"));
            para2.AppendOleObject(s, pic, "mp4");
            //save and view the file
            doc.SaveToFile("Result.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("Result.docx");

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Imports System.IO
Namespace EmbedMediaFile
	Class Program

		Private Shared Sub Main(args As String())
			'create a new Word document and insert section
			Dim doc As New Document()
			Dim section As Section = doc.AddSection()
			'add a paragraph and append some text
			Dim para1 As Paragraph = section.AddParagraph()
			para1.AppendText("Double click the PLAY button to view the video file")
			Dim style1 As New ParagraphStyle(doc)
			style1.Name = "Style"
			style1.CharacterFormat.FontName = "Calibri"
			style1.CharacterFormat.FontSize = 15
			style1.CharacterFormat.Bold = True
			style1.CharacterFormat.TextColor = Color.Red
			doc.Styles.Add(style1)
			para1.ApplyStyle(style1.Name)
			'add another paragraph, append video file as OLE object in Word
			Dim para2 As Paragraph = section.AddParagraph()
			Dim s As Stream = File.OpenRead("media.mp4")
			Dim pic As New DocPicture(doc)
			pic.LoadImage(Image.FromFile("button.png"))
			para2.AppendOleObject(s, pic, "mp4")
			'save and view the file
			doc.SaveToFile("Result.docx", FileFormat.Docx2010)
			System.Diagnostics.Process.Start("Result.docx")

		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Embed Media File in Word
Last modified on Friday, 03 September 2021 04:01