Manage OLE Objects in Presentations Using PHP
Introduction
Object Linking and Embedding (OLE) lets a PowerPoint slide contain an object created in another application, such as a chart from an Excel workbook. An embedded object stores its data in the presentation. A linked object refers to an external file.
PowerPoint can display the object as an icon or show a preview of its contents. Double-clicking the object activates it for editing when the associated application is available. Aspose.Slides for PHP via Java represents both embedded and linked objects as an OleObjectFrame.
Add OLE Object Frames to Slides
To embed an Excel workbook with a chart, pass the workbook bytes and the xlsx extension to OleEmbeddedDataInfo. The extension identifies the file type so PowerPoint can select the associated application. In this example, addOleObjectFrame places the workbook on the first slide and sizes the frame to cover the slide.
use aspose\slides\OleEmbeddedDataInfo;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$slideSize = $presentation->getSlideSize()->getSize();
$files = new JavaClass("java.nio.file.Files");
$workbookFile = new Java("java.io.File", "book.xlsx");
$workbookBytes = $files->readAllBytes($workbookFile->toPath());
$data = new OleEmbeddedDataInfo($workbookBytes, "xlsx");
$slide->getShapes()->addOleObjectFrame(0, 0, $slideSize->getWidth(), $slideSize->getHeight(), $data);
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Add Linked OLE Object Frames
The linked overload of addOleObjectFrame takes an OLE class identifier and the path to the source file. Here, Excel.Sheet.12 identifies an Excel workbook. The example places a 200 × 150-point frame 20 points from the top and left edges of the first slide. The workbook must remain accessible at the stored path for PowerPoint to open it.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$workbookPath = realpath("book.xlsx");
if ($workbookPath === false) {
echo "Cannot locate book.xlsx." . PHP_EOL;
return;
}
$slide->getShapes()->addOleObjectFrame(20, 20, 200, 150, "Excel.Sheet.12", $workbookPath);
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Access OLE Object Frames
Use getEmbeddedData to read the file stored in an embedded OLE frame. This example inspects the first shape on the first slide and prints the file extension and byte count when embedded data is available.
use aspose\slides\Presentation;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || java_values($frame->isObjectLink())) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$data = $frame->getEmbeddedData();
$bytes = java_is_null($data) ? null : $data->getEmbeddedFileData();
$array = new JavaClass("java.lang.reflect.Array");
if (java_is_null($bytes) || java_values($array->getLength($bytes)) == 0) {
echo "The frame contains no embedded file data." . PHP_EOL;
return;
}
printf("Embedded file extension: %s, size: %d bytes.\n", $data->getEmbeddedFileExtension(), java_values($array->getLength($bytes)));
} finally {
$presentation->dispose();
}
Access Linked OLE Object Frame Properties
For a linked frame, isObjectLink returns true and getLinkPathLong identifies the source file. The example prints the stored path for the first shape on the first slide. A PPT presentation can also store a relative path, which the example reads with getLinkPathRelative and prints when available.
use aspose\slides\Presentation;
$presentation = new Presentation("sample.ppt");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not a linked OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || !java_values($frame->isObjectLink())) {
echo "The first shape is not a linked OLE object." . PHP_EOL;
return;
}
echo "Linked file: " . $frame->getLinkPathLong() . PHP_EOL;
$relativePath = java_values($frame->getLinkPathRelative());
if ($relativePath !== null && $relativePath !== "") {
echo "Relative path: " . $relativePath . PHP_EOL;
}
} finally {
$presentation->dispose();
}
Change OLE Object Data
Note
This example uses Aspose.Cells for PHP via Java to edit the embedded workbook.The example expects an embedded XLSX workbook in the first shape on the first slide. It opens the workbook from memory, writes a label and three values to cells E1:E4 on the first worksheet, and saves the workbook back to memory in XLSX format. setEmbeddedData replaces the data stored in the frame so the edited cells are available when the embedded workbook is opened.
use aspose\cells\FileFormatType;
use aspose\cells\Workbook;
use aspose\slides\OleEmbeddedDataInfo;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || java_values($frame->isObjectLink())) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$data = $frame->getEmbeddedData();
$bytes = java_is_null($data) ? null : $data->getEmbeddedFileData();
$array = new JavaClass("java.lang.reflect.Array");
if (java_is_null($bytes) || java_values($array->getLength($bytes)) == 0) {
echo "The frame contains no embedded file data." . PHP_EOL;
return;
}
$extension = (string) $data->getEmbeddedFileExtension();
if (!in_array(strtolower($extension), ["xlsx", ".xlsx"], true)) {
echo "The embedded file is not an XLSX workbook." . PHP_EOL;
return;
}
$input = new Java("java.io.ByteArrayInputStream", $bytes);
$workbook = new Workbook($input);
try {
$cells = $workbook->getWorksheets()->get(0)->getCells();
$cells->get("E1")->putValue("E");
$cells->get("E2")->putValue(12);
$cells->get("E3")->putValue(14);
$cells->get("E4")->putValue(15);
$output = new Java("java.io.ByteArrayOutputStream");
$workbook->save($output, FileFormatType::XLSX);
$updatedBytes = $output->toByteArray();
$updatedData = new OleEmbeddedDataInfo($updatedBytes, "xlsx");
$frame->setEmbeddedData($updatedData);
} finally {
$workbook->dispose();
}
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Embed Other File Types in Slides
You can also embed HTML documents, PDFs, and ZIP archives. This example uses addOleObjectFrame to place an HTML document and a ZIP archive in separate frames on the first slide. Calling setObjectIcon with true displays each file as an icon that the reader can double-click to open in an associated application.
use aspose\slides\OleEmbeddedDataInfo;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$files = new JavaClass("java.nio.file.Files");
$htmlFile = new Java("java.io.File", "sample.html");
$htmlBytes = $files->readAllBytes($htmlFile->toPath());
$htmlData = new OleEmbeddedDataInfo($htmlBytes, "html");
$htmlFrame = $slide->getShapes()->addOleObjectFrame(150, 120, 50, 50, $htmlData);
$htmlFrame->setObjectIcon(true);
$zipFile = new Java("java.io.File", "sample.zip");
$zipBytes = $files->readAllBytes($zipFile->toPath());
$zipData = new OleEmbeddedDataInfo($zipBytes, "zip");
$zipFrame = $slide->getShapes()->addOleObjectFrame(150, 220, 50, 50, $zipData);
$zipFrame->setObjectIcon(true);
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set File Types for Embedded Objects
The extension in OleEmbeddedDataInfo describes the format of the supplied bytes. If the first shape on the first slide already contains a ZIP archive with an incorrect extension, use setEmbeddedData to correct its type. The example prints the current extension and saves the same archive bytes with the zip extension.
use aspose\slides\OleEmbeddedDataInfo;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || java_values($frame->isObjectLink())) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$data = $frame->getEmbeddedData();
$bytes = java_is_null($data) ? null : $data->getEmbeddedFileData();
$array = new JavaClass("java.lang.reflect.Array");
if (java_is_null($bytes) || java_values($array->getLength($bytes)) == 0) {
echo "The frame contains no embedded file data." . PHP_EOL;
return;
}
echo "Current embedded file extension: " . $data->getEmbeddedFileExtension() . PHP_EOL;
$archiveData = new OleEmbeddedDataInfo($bytes, "zip");
$frame->setEmbeddedData($archiveData);
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set Icon Images and Titles for Embedded Objects
To customize the icon for an embedded object, assign an image from the presentation’s image collection through the picture format returned by getSubstitutePictureFormat and set the caption with setSubstitutePictureTitle. The example uses a PNG image for the first shape on the first slide and calls setObjectIcon with true to display the icon and caption.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || java_values($frame->isObjectLink())) {
echo "The first shape is not an embedded OLE object." . PHP_EOL;
return;
}
$files = new JavaClass("java.nio.file.Files");
$imageFile = new Java("java.io.File", "image.png");
$imageBytes = $files->readAllBytes($imageFile->toPath());
$iconImage = $presentation->getImages()->addImage($imageBytes);
$frame->setObjectIcon(true);
$frame->getSubstitutePictureFormat()->getPicture()->setImage($iconImage);
$frame->setSubstitutePictureTitle("Embedded workbook");
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Prevent an OLE Object Frame from Being Resized and Repositioned
When PowerPoint refreshes a linked object’s preview, the frame can move or change size. Call setUpdateAutomatic with false to request manual updates for that link. The example applies this setting to the first shape on the first slide when it is a linked OLE frame.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
if (java_values($shapes->size()) == 0) {
echo "The first shape is not a linked OLE object." . PHP_EOL;
return;
}
$frame = $shapes->get_Item(0);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || !java_values($frame->isObjectLink())) {
echo "The first shape is not a linked OLE object." . PHP_EOL;
return;
}
$frame->setUpdateAutomatic(false);
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Extract Embedded Files
Read getEmbeddedFileData to extract the original bytes and getEmbeddedFileExtension to name the output. This example scans the top-level shapes on the first slide, skips linked objects, and writes each embedded file to the current directory. The shape index distinguishes files with the same extension.
use aspose\slides\Presentation;
$presentation = new Presentation("sample.pptx");
try {
$shapes = $presentation->getSlides()->get_Item(0)->getShapes();
$extracted = 0;
$shapeCount = java_values($shapes->size());
$array = new JavaClass("java.lang.reflect.Array");
for ($index = 0; $index < $shapeCount; $index++) {
$frame = $shapes->get_Item($index);
if (!java_instanceof($frame, new JavaClass("com.aspose.slides.OleObjectFrame")) || java_values($frame->isObjectLink())) {
continue;
}
$data = $frame->getEmbeddedData();
$bytes = java_is_null($data) ? null : $data->getEmbeddedFileData();
if (java_is_null($bytes) || java_values($array->getLength($bytes)) == 0) {
printf("Shape %d contains no embedded file data.\n", $index);
continue;
}
$extension = (string) java_values($data->getEmbeddedFileExtension());
$suffix = ltrim($extension, ".");
$fileName = "OLE_object_" . $index . ($suffix === "" ? "" : "." . $suffix);
$output = new Java("java.io.FileOutputStream", $fileName);
try {
$output->write($bytes);
} finally {
$output->close();
}
$extracted++;
}
printf("Extracted %d embedded files.\n", $extracted);
} finally {
$presentation->dispose();
}
List Embedded and Linked OLE Objects
The getObjectProgId method identifies the OLE object type associated with an application. For example, Word.Document.12 identifies a Word document. The embedded file extension is a separate value. The ProgID alone cannot confirm the format or validity of the stored data.
The example scans every slide in sample.pptx and follows GroupShape::getShapes into nested groups. For each OleObjectFrame, it prints the slide number, shape name, isObjectLink, and the original ProgID. An unfamiliar ProgID appears as stored. An empty ProgID leaves the last column blank. The example reads only metadata, without extracting, opening, or activating the OLE content.
use aspose\slides\Presentation;
$presentation = new Presentation("sample.pptx");
try {
$reportObjects = function ($shapes, $slideNumber) use (&$reportObjects) {
$shapeCount = java_values($shapes->size());
for ($i = 0; $i < $shapeCount; $i++) {
$shape = $shapes->get_Item($i);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.OleObjectFrame"))) {
$progId = java_values($shape->getObjectProgId());
$linked = java_values($shape->isObjectLink());
printf("%d | %s | %s | %s\n", $slideNumber, $shape->getName(), $linked ? "true" : "false", $progId ?? "");
} elseif (java_instanceof($shape, new JavaClass("com.aspose.slides.GroupShape"))) {
$reportObjects($shape->getShapes(), $slideNumber);
}
}
};
echo "Slide | Shape | IsObjectLink | ObjectProgId" . PHP_EOL;
$slides = $presentation->getSlides();
$slideCount = java_values($slides->size());
for ($i = 0; $i < $slideCount; $i++) {
$slide = $slides->get_Item($i);
$reportObjects($slide->getShapes(), java_values($slide->getSlideNumber()));
}
} finally {
$presentation->dispose();
}
FAQ
What appears when I export an OLE object to PDF or an image?
The export shows the icon or preview image visible on the slide. To control its appearance, set a custom preview before exporting. To include the embedded file as a PDF attachment, call PdfOptions::setIncludeOleData with true. This option is disabled by default. See Preserve Embedded OLE Files as PDF Attachments.
How can I prevent accidental changes to an OLE frame in PowerPoint?
Apply shape-level locks to restrict actions such as moving or resizing the frame.
Why does a linked Excel object move or change size when I open the presentation?
PowerPoint may refresh the preview using the dimensions of the linked worksheet range. To keep the dimensions consistent, fit the frame to the range or scale the range to a fixed frame and provide a matching preview. See Working Solution for Worksheet Resizing.
Can a PPTX presentation store a relative path for a linked OLE object?
The relative path is available only in the older PPT format. For PPTX, use a full path that recipients can access or embed the file in the presentation.