使用 OLE 对象和在线视频
OLE(对象链接和嵌入)是一种技术,用户可以通过该技术处理包含由第三方应用程序创建或编辑的"对象"的文档。也就是说,OLE 允许编辑应用程序将这些"对象"导出到另一个编辑应用程序,然后将它们与附加内容一起导入。
在本文中,我们将讨论插入 OLE 对象并设置其属性,以及将在线视频插入到文档中。
插入 OLE 对象
如果您需要 OLE 对象,请调用 InsertOleObject 方法并将 ProgId 与其他参数显式传递给它。
以下代码示例显示如何将 OLE 对象插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertOleObject("http://www.aspose.com", "htmlfile", true, true, null); | |
dataDir = dataDir + "DocumentBuilderInsertOleObject_out.doc"; | |
doc.Save(dataDir); |
插入 OLE 对象时设置文件名和扩展名
如果 OLE 处理程序未知,OLE 包是一种传统且"未记录"的存储嵌入对象的方法。
早期的 Windows 版本(例如 Windows 3.1、95 和 98)具有 Packager.exe 应用程序,可用于将任何类型的数据嵌入到文档中。该应用程序现已从 Windows 中排除,但如果 OLE 处理程序丢失或未知,Microsoft Word 和其他应用程序仍使用它来嵌入数据。 OlePackage
类允许用户访问 OLE 包属性。
以下代码示例显示如何设置 OLE 包的文件名、扩展名和显示名称:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
byte[] bs = File.ReadAllBytes(dataDir + @"input.zip"); | |
using (Stream stream = new MemoryStream(bs)) | |
{ | |
Shape shape = builder.InsertOleObject(stream, "Package", true, null); | |
OlePackage olePackage = shape.OleFormat.OlePackage; | |
olePackage.FileName = "filename.zip"; | |
olePackage.DisplayName = "displayname.zip"; | |
dataDir = dataDir + "DocumentBuilderInsertOleObjectOlePackage_out.doc"; | |
doc.Save(dataDir); | |
} | |
访问 OLE 对象原始数据
用户可以使用 OleFormat
类的各种属性和方法访问 OLE 对象数据。例如,可以获得 OLE
对象原始数据或链接的 OLE 对象的源文件的路径和名称。
以下代码示例演示如何使用 GetRawData 方法获取 OLE 对象原始数据:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Load document with OLE object. | |
Document doc = new Document(dataDir + "DocumentBuilderInsertTextInputFormField_out.doc"); | |
Shape oleShape = (Shape)doc.GetChild(NodeType.Shape, 0, true); | |
byte[] oleRawData = oleShape.OleFormat.GetRawData(); |
插入 OLE 对象作为图标
OLE 对象也可以作为图像插入到文档中。
以下代码示例显示如何插入 OLE 对象作为图标。为此,DocumentBuilder 类公开了 InsertOleObjectAsIcon 方法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file"); | |
doc.Save(dataDir + "EmbeddeWithIcon_out.docx"); | |
Console.WriteLine("The document has been saved with OLE Object as an Icon."); |
以下代码示例演示如何将嵌入的 OLE 对象作为图标从流插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file"); | |
using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(dataDir + "embedded.xlsx"))) | |
builder.InsertOleObjectAsIcon(stream, "Package", dataDir + "icon.ico", "My embedded file"); | |
doc.Save(dataDir + "EmbeddeWithIconUsingStream_out.docx"); | |
Console.WriteLine("The document has been saved with OLE Object as an Icon."); |
插入在线视频
可以通过*“插入”>“在线视频”*选项卡将在线视频插入到Word文档中。您可以通过调用InsertOnlineVideo方法将在线视频插入到当前位置的文档中。
DocumentBuilder 类引入了此方法的四个重载。第一个适用于最流行的视频资源,并以视频的 URL
作为参数。例如,第一个重载支持从 YouTube 和 维梅奥 资源简单插入在线视频。
以下代码示例演示如何将 Vimeo 中的在线视频插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
//The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Pass direct url from youtu.be. | |
string url = "https://youtu.be/t_1LYZ102RA"; | |
double width = 360; | |
double height = 270; | |
Shape shape = builder.InsertOnlineVideo(url, width, height); | |
dataDir = dataDir + "Insert.OnlineVideo_out_.docx"; | |
doc.Save(dataDir); |
第二个重载适用于所有其他视频资源,并采用嵌入的 HTML 代码作为参数。用于嵌入视频的 HTML 代码可能因提供商而异,因此请联系相应的提供商了解详细信息。
以下代码示例展示了如何使用此类 HTML 代码将在线视频插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
//The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Shape width/height. | |
double width = 360; | |
double height = 270; | |
// Poster frame image. | |
byte[] imageBytes = File.ReadAllBytes("TestImage.jpg"); | |
// Visible url | |
string vimeoVideoUrl = @"https://vimeo.com/52477838"; | |
// Embed Html code. | |
string vimeoEmbedCode = ""; | |
builder.InsertOnlineVideo(vimeoVideoUrl, vimeoEmbedCode, imageBytes, width, height); | |
dataDir = dataDir + "Insert.OnlineVideo_out_.docx"; | |
doc.Save(dataDir); |