翻譯|使用教程|編輯:胡濤|2023-06-08 10:24:29.867|閱讀 129 次
概述:本文介紹如何在 Word 中嵌入多媒體文件
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for .NET 支持將 Office(Word/Excel/PowerPoint)、PDF、txt 等文件作為 OLE 對(duì)象插入到 Word 文檔,雙擊該對(duì)象可直接訪問(wèn)或編輯該文件;除了以上常見(jiàn)的文件格式對(duì)象,也支持插入多媒體文件,如視頻、音頻等,本文將對(duì)此作詳細(xì)介紹。
首先,您需要將 Spire.Doc for.NET 包含的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中的 DLL 文件可以從此鏈接下載,也可以通過(guò) NuGet 安裝。
PM> Install-Package Spire.Doc
代碼中嵌入多媒體文件的方法是通過(guò)調(diào)用 AppendOleObject(Stream oleStream, DocPicture olePicture, string fileExtension) 方法來(lái)實(shí)現(xiàn),該方法中的三個(gè)參數(shù)解釋分別為:
主要代碼步驟如下:
【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) { //實(shí)例化Document類的對(duì)象,并添加Section Document doc = new Document(); Section section = doc.AddSection(); //定義段落樣式 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); //添加段落1,嵌入視頻文件 Paragraph para1 = section.AddParagraph(); para1.AppendText("嵌入視頻文件:"); 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"); //添加一個(gè)空白段落2 Paragraph para2 = section.AddParagraph(); //添加段落3,嵌入音頻文件 Paragraph para3 = section.AddParagraph(); para3.AppendText("嵌入音頻文件:"); 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"); //保存文檔 doc.SaveToFile("Result.docx", FileFormat.Docx2013); } } }
【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()) '實(shí)例化Document類的對(duì)象,并添加Section Dim doc As New Document() Dim section As Section = doc.AddSection() '定義段落樣式 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) '添加段落1,嵌入視頻文件 Dim para1 As Paragraph = section.AddParagraph() para1.AppendText("嵌入視頻文件:") 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") '添加一個(gè)空白段落2 Dim para2 As Paragraph = section.AddParagraph() '添加段落3,嵌入音頻文件 Dim para3 As Paragraph = section.AddParagraph() para3.AppendText("嵌入音頻文件:") 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") '保存文檔 doc.SaveToFile("Result.docx", FileFormat.Docx2013) End Sub End Class End Namespace
嵌入后的文檔效果:
以上便是如何在 Word 中嵌入多媒體文件,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn