使用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-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOleObject(u"http://www.aspose.com", u"htmlfile", true, true, nullptr);
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObject.docx");

插入OLE对象时设置文件名和扩展名

如果OLE处理程序未知,OLE包是存储嵌入对象的传统和"未记录"方式。

早期的Windows版本,如Windows3.1,95和98有一个Packager。exe应用程序,可用于将任何类型的数据嵌入到文档中。 此应用程序现在从Windows中排除,但Microsoft Word和其他应用程序仍然使用它来嵌入数据,如果OLE处理程序丢失或未知。 OlePackage类允许用户访问OLE包属性。

下面的代码示例演示如何设置OLE包的文件名、扩展名和显示名称:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
ArrayPtr<uint8_t> bs = System::IO::File::ReadAllBytes(MyDir + u"Zip file.zip");
{
SharedPtr<System::IO::Stream> stream = MakeObject<System::IO::MemoryStream>(bs);
SharedPtr<Shape> shape = builder->InsertOleObject(stream, u"Package", true, nullptr);
SharedPtr<OlePackage> olePackage = shape->get_OleFormat()->get_OlePackage();
olePackage->set_FileName(u"filename.zip");
olePackage->set_DisplayName(u"displayname.zip");
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectWithOlePackage.docx");
}

获取对OLE对象原始数据的访问

用户可以使用OleFormat类的各种属性和方法访问OLE对象数据。 例如,可以获取OLE对象原始数据或链接的OLE对象的源文件的路径和名称。

下面的代码示例演示如何使用GetRawData方法获取OLE对象原始数据:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto oleShape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
ArrayPtr<uint8_t> oleRawData = oleShape->get_OleFormat()->GetRawData();

插入Ole对象作为图标

OLE对象也可以作为图像插入到文档中。

下面的代码示例演示如何将OLE对象作为图标插入。 为此,DocumentBuilder类公开InsertOleObjectAsIcon方法:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOleObjectAsIcon(MyDir + u"Presentation.pptx", false, ImagesDir + u"Logo icon.ico", u"My embedded file");
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIcon.docx");

下面的代码示例演示如何将嵌入的OLE对象作为流中的图标插入到文档中:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
{
auto stream = MakeObject<System::IO::MemoryStream>(System::IO::File::ReadAllBytes(MyDir + u"Presentation.pptx"));
builder->InsertOleObjectAsIcon(stream, u"Package", ImagesDir + u"Logo icon.ico", u"My embedded file");
}
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIconUsingStream.docx");

插入在线视频

在线视频可以从*“Insert” > “Online Video”*选项卡插入到Word文档中。 您可以通过调用InsertOnlineVideo方法将在线视频插入到当前位置的文档中。

DocumentBuilder类引入了此方法的四个重载。 第一个使用最流行的视频资源,并将视频的URL作为参数。 例如,第一个重载支持从简单的插入在线视频 YouTube维梅奥 资源。

下面的代码示例演示如何将Vimeo中的在线视频插入到文档中:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOnlineVideo(u"https://youtu.be/t_1LYZ102RA", 360, 270);
// We can watch the video from Microsoft Word by clicking on the shape.
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertVideoWithUrl.docx");

第二个重载与所有其他视频资源一起工作,并将嵌入的HTML代码作为参数。 嵌入视频的HTML代码可能因提供商而异,因此请联系相应的提供商以获取详细信息。

下面的代码示例演示如何使用这样的HTML代码将在线视频插入到文档中:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
String videoUrl = u"https://vimeo.com/52477838";
String videoEmbedCode = String(u"<iframe src=\"https://player.vimeo.com/video/52477838\" width=\"640\" height=\"360\" frameborder=\"0\" ") +
u"title=\"Aspose\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
ArrayPtr<uint8_t> thumbnailImageBytes = System::IO::File::ReadAllBytes(ImageDir + u"Logo.jpg");
{
auto stream = MakeObject<System::IO::MemoryStream>(thumbnailImageBytes);
{
SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromStream(stream);
// Below are two ways of creating a shape with a custom thumbnail, which links to an online video
// that will play when we click on the shape in Microsoft Word.
// 1 - Insert an inline shape at the builder's node insertion cursor:
builder->InsertOnlineVideo(videoUrl, videoEmbedCode, thumbnailImageBytes, image->get_Width(), image->get_Height());
}
}
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertOnlineVideoCustomThumbnail.docx");