Introduction
OLE stands for Object Linking & Embedding. It's a Microsoft technology that allows objects created in one application to be embedded in another application. For example, you can create a chart in an Excel Worksheet and then embed that chart object into your PowerPoint slide. After the chart object is embedded, you just double click the object and the chart object will be opened in editable form as you see in MS Excel. Aspose.Slides supports adding OLE Objects to the slides in the form of OLE Object Frames. In this topic, we will work with OLE Object Frames to see that how can we add and access these objects to and from slides using Aspose.Slides.
Working with OLE Object Frames
Adding an OLE Object Frame to a Slide
Suppose, you have created a Microsoft Excel Chart in an Excel file and want to embed that chart object in a slide as an OLE Object Frame using Aspose.Slides. Then you can do that using the steps below:
- Create an instance of Presentation class
- Obtain the reference of a slide by using its Position
- Open the Excel file containing Microsoft Excel Chart object and save it as an array of bytes
- Add the OLE Object Frame to the slide containing the array of bytes and other information about the OLE object
- Write the modified presentation as a PPT file
In the example given below, a Microsoft Excel Chart object in an Excel file is added to a slide as an OLE Object Frame using Aspose.Slides. The Excel file (excel1.xls) that was used in this example can be downloaded from here.
Example:
[C#]
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation("C:\\demo.ppt");
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(2);
//Reading excel chart from the excel file and save as an array of bytes
FileStream fstro = new FileStream("C:\\excel1.xls", FileMode.Open,FileAccess.Read);
byte[] b = new byte[fstro.Length];
fstro.Read(b, 0, (int)fstro.Length);
//Inserting the excel chart as new OleObjectFrame to a slide
OleObjectFrame oof = slide.Shapes.AddOleObjectFrame(0,0,pres.SlideSize.Width,
pres.SlideSize.Height,"Excel.Sheet.8", b);
//Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt");
[VB.NET]
'Instantiate a Presentation object that represents a PPT file
Dim pres As Presentation = New Presentation("C:\\demo.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(2)
'Reading excel chart from the excel file and save as an array of bytes
Dim fstro As FileStream = New FileStream("C:\\excel1.xls",FileMode.Open,FileAccess.Read)
Dim b() As Byte = New Byte(fstro.Length) {}
fstro.Read(b, 0, CType(fstro.Length, Integer))
'Inserting the excel chart as new OleObjectFrame to a slide
OleObjectFrame oof = slide.Shapes.AddOleObjectFrame(0,0,pres.SlideSize.Width,
pres.SlideSize.Height,"Excel.Sheet.8", b)
'Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt")
[Java]
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation(new FileInputStream(new File("demo.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(2);
//Reading excel chart from the excel file and save as an array of bytes
File file=new File("C:\\excel1.xls");
int length=(int)file.length();
FileInputStream fstro = new FileInputStream(file);
byte[] b = new byte[length];
fstro.read(b, 0, length);
//Inserting the excel chart as new OleObjectFrame to a slide
OleObjectFrame oof = slide.getShapes().addOleObjectFrame(0,0,(int)pres.getSlideSize().getX(),
(int)pres.getSlideSize().getY(),"Excel.Sheet.8", b);
//Writing the presentation as a PPT file
pres.write(new FileOutputStream(new File("modified.ppt")));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(2);
//Reading excel chart from the excel file and save as an array of bytes
$b = file_get_contents("C:\\excel1.xls");
//Inserting the excel chart as new OleObjectFrame to a slide
$oof = $slide->getShapes()->addOleObjectFrame(0,0,$pres->getSlideSize()->getX(),
$pres->getSlideSize()->getY(),"Excel.Sheet.8", $b);
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres->write($fostream);
//Closing the streams
$fistream->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>
The above code snippet adds an OLE Object Frame to a slide as shown below in the figure:
|
Figure: Microsoft Excel Chart object added to a slide
|
Note: For more details about Object Changed issue, please click here.
Accessing an OLE Object Frame from a Slide
If an OLE object is already embedded in a slide, you can access that object easily using Aspose.Slides. Please follow the steps below to find or access an OLE object from a slide:
- Create an instance of Presentation class
- Obtain the reference of a slide by using its Position
- Find the OLE Object (embedded in the slide) through its Alternative Text. Once the object is found, typecast that object as an OLE Object Frame. This was the desired OLE Object Frame to be accessed
- Once the OLE Object Frame is accessed, you can perform any operation on it
In the example given below, an OLE Object Frame (that is a Microsoft Excel Chart object embedded in a slide) is accessed and then all of its Object Data is written to an Excel file. The PPT file (chart.ppt) that was used in this example can be downloaded from here.
Example:
[C#]
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation("C:\\chart.ppt");
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(1);
//Finding excel chart shape and obtaining its reference as OleObjectFrame
OleObjectFrame oof = slide.FindShape("chart") as OleObjectFrame;
//Check, if OleObjectFrame is not null then read the object data of
//OleObjectFrame as an array of bytes. Then write those bytes as an excel file
if (oof != null)
{
FileStream fstr = new FileStream("C:\\excel1.xls", FileMode.Create, FileAccess.Write);
byte[] buf = oof.ObjectData;
fstr.Write(buf, 0, buf.Length);
fstr.Flush();
fstr.Close();
System.Console.WriteLine("Excel OLE Object written as excel1.xls file");
}
[VB.NET]
'Instantiate a Presentation object that represents a PPT file
Dim pres As Presentation = New Presentation("C:\\chart.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(1)
'Finding excel chart shape and obtaining its reference as OleObjectFrame
Dim oof As OleObjectFrame = slide.FindShape("chart") as OleObjectFrame
'Check, if OleObjectFrame is not null then read the object data of
'OleObjectFrame as an array of bytes. Then write those bytes as an excel file
If Not oof Is Nothing Then
Dim fstr As FileStream = New FileStream("C:\\excel1.xls",FileMode.Create,FileAccess.Write)
Dim buf() As Byte = oof.ObjectData
fstr.Write(buf, 0, buf.Length)
fstr.Flush()
fstr.Close()
System.Console.WriteLine("Excel OLE Object written as excel1.xls file")
End If
[Java]
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation(new FileInputStream(new File("demo.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(1);
//Finding excel chart shape and obtaining its reference as OleObjectFrame
OleObjectFrame oof = (OleObjectFrame) slide.findShape("chart");
//Check, if OleObjectFrame is not null then read the object data of
//OleObjectFrame as an array of bytes. Then write those bytes as an excel file
if (oof != null)
{
FileOutputStream fstr = new FileOutputStream("C:\\excel1.xls");
byte[] buf = oof.getObjectData();
fstr.write(buf, 0, buf.length);
fstr.flush();
fstr.close();
System.out.println("Excel OLE Object written as excel1.xls file");
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(1);
//Finding excel chart shape using its alternate text and obtaining its
//reference as OleObjectFrame
$oof = $slide->findShape("chart");
//Check, if OleObjectFrame is not null then read the object data of
//OleObjectFrame as an array of bytes. Then write those bytes as an excel file
if ($oof != null)
{
$fstr = new Java("java.io.FileOutputStream","C:\\excel1.xls");
$buf = $oof->getObjectData();
$fstr->write($buf, 0, strlen($buf));
$fstr->flush();
$fstr->close();
echo "Excel OLE Object written as excel1.xls file";
}
//Closing the stream
$fistream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>