使用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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObject("http://www.aspose.com", "htmlfile", true, true, null);
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObject.docx");

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
byte[] bs = FileUtils.readFileToByteArray(new File(getMyDir() + "Zip file.zip"));
try (ByteArrayInputStream stream = new ByteArrayInputStream(bs))
{
Shape shape = builder.insertOleObject(stream, "Package", true, null);
OlePackage olePackage = shape.getOleFormat().getOlePackage();
olePackage.setFileName("filename.zip");
olePackage.setDisplayName("displayname.zip");
doc.save(getArtifactsDir() + "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-Java
Shape oleShape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
byte[] oleRawData = oleShape.getOleFormat().getRawData();

插入OLE对象作为图标

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObjectAsIcon(getMyDir() + "Presentation.pptx", false, getImagesDir() + "Logo icon.ico",
"My embedded file");
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIcon.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
try(ByteArrayInputStream stream = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(getMyDir() + "Presentation.pptx"))))
{
builder.insertOleObjectAsIcon(stream, "Package", getImagesDir() + "Logo icon.ico", "My embedded file");
}
doc.save(getArtifactsDir() + "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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOnlineVideo("https://youtu.be/t_1LYZ102RA", 360.0, 270.0);
// We can watch the video from Microsoft Word by clicking on the shape.
doc.save(getArtifactsDir() + "DocumentBuilder.InsertVideoWithUrl.docx");

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
String videoUrl = "https://vimeo.com/52477838";
String videoEmbedCode = "<iframe src=\"https://player.vimeo.com/video/52477838\" width=\"640\" height=\"360\" frameborder=\"0\" " +
"title=\"Aspose\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
byte[] thumbnailImageBytes = IOUtils.toByteArray(getAsposelogoUri().toURL().openStream());
BufferedImage image = ImageIO.read(new ByteArrayInputStream(thumbnailImageBytes));
// 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.getWidth(), image.getHeight());
doc.save(getArtifactsDir() + "DocumentBuilder.InsertOnlineVideoCustomThumbnail.docx");