翻譯|使用教程|編輯:胡濤|2023-05-31 11:06:36.477|閱讀 100 次
概述:在本文中,您將學習如何在 C#、VB.NET 中使用 Spire.Doc 將媒體文件(音頻或視頻)添加到 Word 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc 支持將任何類型的文件(如Excel、PDF、PowerPoint 等)作為 OLE 對象插入到 Word 文檔中。在本文中,您將學習如何在 C#、VB.NET 中使用 Spire.Doc 將媒體文件(音頻或視頻)添加到 Word 文檔。
在 DocOleObject 類中,有一個名為AppendOleObject(Stream oleStream, DocPicture olePicture, string fileExtension)的方法可供用戶將擴展名為 mp3、mp4、avi 或任何其他格式的媒體文件插入到 Word 文檔中。該方法中的三個參數分別代表:
第 1 步:初始化 Document 類的一個新實例并添加一個新部分。
Document doc = new Document(); Section section = doc.AddSection();
第 2 步:添加一個新段落,在段落中附加一些格式化的文本。
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);
第 3 步:添加另一個段落,將視頻文件作為 OLE 對象附加到段落中。
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");
第 4 步:保存視圖文件。
doc.SaveToFile("Result.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("Result.docx");
輸出:
完整代碼:
[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
以上便是在 Word 中插入上標和下標,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn