Get the App
SLTechnology News&Howtos  ›  Development  › 

How to embed Multimedia Files in Word by VB.NET in C #

Shulou Source: shulou.com Published: 2022-06-02 07:05:08 10月04日 Update

Editor to share with you how to embed multimedia files in Word in VB.NET in C#, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Introduction of dll

1. Install dll via NuGet (2 ways)

You can open solution Explorer in Visual Studio, right-click "references", "manage NuGet packages", and then search for Free Spire.Doc installation.

1.2 copy the following to the PM console installation.

Install-Package FreeSpire.Doc-Version 9.9.7

two。 Manually add a dll reference

You can download the package manually, and then extract it to find the Spire.Doc.dll in the BIN folder. Open solution Explorer in Visual Studio, right-click reference, and add reference to the dll file under the local path BIN folder to the program.

Embed multimedia files

The method of embedding multimedia files in the code is realized by calling the AppendOleObject (Stream oleStream, DocPicture olePicture, string fileExtension) method. The three parameters in this method are interpreted as follows:

OleStream: OLE file stream

OlePicture: used to display images (icons) of OLE objects

FileExtension: embedded file object extension (e.g. mp3, mp4, avi, etc.)

Parsing of main code steps

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

two。 Add a paragraph and call the Paragraph.AppendOleObject () method to embed the multimedia file into the paragraph as an OLE object.

3. Save the document to the specified path through Document.SaveToFile (string fileName, FileFormat fileFormat).

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) {/ / instantiate the object of the Document class and add Section Document doc = new Document (); Section section = doc.AddSection () / / define paragraph styles ParagraphStyle style1 = new ParagraphStyle (doc); style1.Name = "Style"; style1.CharacterFormat.FontName = "Calibri"; style1.CharacterFormat.FontSize = 18; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.BlueViolet; doc.Styles.Add (style1) / / add paragraph 1, embed the video file Paragraph para1 = section.AddParagraph (); para1.AppendText ("embed Video File:"); para1.ApplyStyle (style1.Name); Stream S1 = File.OpenRead ("Video.mp4"); DocPicture pic1 = new DocPicture (doc) Pic1.LoadImage (Image.FromFile ("logo1.png")); para1.AppendOleObject (S1, pic1, "mp4"); / / add a blank paragraph 2 Paragraph para2 = section.AddParagraph (); / / add paragraph 3, embed the audio file Paragraph para3 = section.AddParagraph (); para3.AppendText ("embed audio file:") Para3.ApplyStyle (style1.Name); Stream S2 = File.OpenRead ("Audio.mp3"); DocPicture pic2 = new DocPicture (doc); pic2.LoadImage (Image.FromFile ("logo2.png")); para3.AppendOleObject (S2, pic2, "mp3"); / / Save document doc.SaveToFile ("Result.docx", FileFormat.Docx2013) }}}

VB.NET

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingImports System.IONamespace EmbedMediaFile Class Program Private Shared Sub Main (args As String ()) 'instantiates the object of the Document class And add Section Dim doc As New Document () Dim section As Section = doc.AddSection ()'to define the paragraph style Dim style1 As New ParagraphStyle (doc) style1.Name = "Style" style1.CharacterFormat.FontName = "Calibri" style1.CharacterFormat.FontSize = 18 style1.CharacterFormat.Bold = True style1.CharacterFormat .TextColor = Color.BlueViolet doc.Styles.Add (style1) 'add paragraph 1 Embedded video file Dim para1 As Paragraph = section.AddParagraph () para1.AppendText ("embedded video file:") para1.ApplyStyle (style1.Name) Dim S1 As Stream = File.OpenRead ("Video.mp4") Dim pic1 As New DocPicture (doc) pic1.LoadImage (Image.FromFile ("logo1.png")) para1.AppendOleObject (S1, pic1 "mp4") 'add a blank paragraph 2 Dim para2 As Paragraph = section.AddParagraph ()' add paragraph 3 Embed audio file Dim para3 As Paragraph = section.AddParagraph () para3.AppendText ("embed audio file:") para3.ApplyStyle (style1.Name) Dim S2 As Stream = File.OpenRead ("Audio.mp3") Dim pic2 As New DocPicture (doc) pic2.LoadImage (Image.FromFile ("logo2.png")) para3.AppendOleObject (S2, pic2 "mp3") 'saves the document effect after doc.SaveToFile ("Result.docx", FileFormat.Docx2013) End Sub End ClassEnd Namespace embedding effect

Matters needing attention

All the file paths in the code are the Debug paths of the VS program, such as: F:\ VS2017Project\ InsertOLE_Doc\ EmbedMediaFile\ bin\ Debug\ Result.docx, and the file path is customized to another path.

The above code program introduces dll in the version of the free Word library Free Spire.Doc for .NET.

The above is all the contents of the article "how to embed multimedia files in Word with VB.NET in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files paragraphs paths multimedia objects methods code documents videos audio content examples programs articles management clips manual effects folders schemes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi Shulou Information Apple NVidia