Working with OLE Objects and Online Video
OLE (Object Linking and Embedding) is a technology by which users can work with documents containing “objects” created or edited by third-party applications. That is, OLE allows an editing application to export these “objects” to another editing application and then import them with additional content.
In this article, we will talk about inserting an OLE object and setting its properties, as well as inserting an online video into a document.
Insert OLE Object
If you want OLE Object, call the InsertOleObject method and pass it the ProgId explicitly with other parameters.
The following code example shows how to insert OLE Object into a document:
// 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); |
Set File Name and Extension when Inserting OLE Object
OLE package is a legacy and “undocumented” way to store embedded objects if an OLE handler is unknown.
Early Windows versions such as Windows 3.1, 95, and 98 had a Packager.exe application that could be used to embed any type of data into the document. This application is now excluded from Windows, but Microsoft Word and other applications still use it to embed data if the OLE handler is missing or unknown. The OlePackage class allows users to access the OLE Package properties.
The following code example shows how to set the file name, extension, and display name for OLE Package:
// 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); | |
} | |
Get Access to OLE Object Raw Data
Users can access OLE object data using various properties and methods of the OleFormat class. For example, it is possible to get the OLE object raw data or the path and name of a source file for the linked OLE object.
The following code example shows how to get OLE Object raw data using the GetRawData method:
// 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(); |
Insert OLE Object as an Icon
OLE objects can also be inserted into documents as images.
The following code example shows how to insert OLE Object as an icon. For this purpose, the DocumentBuilder class exposes the InsertOleObjectAsIcon method:
// 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."); |
The following code example shows how to inserts an embedded OLE object as an icon from a stream into the document:
// 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."); |
Insert Online Video
Online video can be inserted into Word document from the “Insert” > “Online Video” tab. You can insert an online video into a document at the current location by calling the InsertOnlineVideo method.
The DocumentBuilder class introduces four overloads of this method. The first one works with the most popular video resources and takes the URL of the video as a parameter. For example, the first overload supports simple insertion of online videos from YouTube and Vimeo resources.
The following code example shows how to insert an online video from Vimeo into a document:
// 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); |
The second overload works with all other video resources and takes embedded HTML code as a parameter. The HTML code for embedding a video may vary depending on the provider, so contact the respective provider for details.
The following code example shows how to insert an online video into a document using such HTML code:
// 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); |