DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to working with OLE object in Presentation slides in Java applications

When the slides are used for product demonstration, we usually add other document files (such as PDF, Excel or Word) as an OLE object into our slides to offer more details for our presentation. This article I will demonstrate how to use Spire.Presentation for Java to embed the entire PDF file into the presentation and extract the OLE objects from the presentation in Java applications.

Embed a PDF into the presentation as an object:

import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class embedPDFAsOLE  {
    public static void main(String[] args) throws Exception{

        Presentation ppt = new Presentation();
        //Load the image file
        File file =new File("PDFlogo.jpg");
        BufferedImage image = ImageIO.read(file);
        IImageData oleImage = ppt.getImages().append(image);
        Rectangle rec = new Rectangle(80, 60, image.getWidth(), image.getHeight());

        String input = "Sample.pdf";
        File oldFile=new File(input);
        FileInputStream inputStream = new FileInputStream(oldFile);
        byte[] data = new byte[(int)oldFile.length()];
        inputStream.read(data,0,data.length);

        //Insert an OLE object to presentation based on the PDF data
        com.spire.presentation.IOleObject oleObject=ppt.getSlides().get(0).getShapes().appendOleObject("pdf", data, rec);
        oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);

        oleObject.setProgId("AcroExch.Document.11");
        //Save to file.
        ppt.saveToFile("output/embedPDFAsOLE.pptx", FileFormat.PPTX_2013);
    }
Enter fullscreen mode Exit fullscreen mode

Effective screenshot after embedding PDF into Presentation slides as an object:
Embed PDF as OLE object

Extract the OLE objects from the presentation slides:

import com.spire.presentation.*;
import java.io.FileOutputStream;

public class extractOLEObject {
    public static void main(String[] args) throws Exception {

        //Create a PPT document
        Presentation presentation = new Presentation();

        //Load document from disk
        presentation.loadFromFile("output/embedPDFAsOLE.pptx");

        //Loop through the slides and shapes
        for (Object slideObj : presentation.getSlides()) {
            ISlide slide = (ISlide) slideObj;
            for (Object shapeObj : slide.getShapes()) {
                IShape shape = (IShape) shapeObj;
                if (shape instanceof IOleObject) {
                    //Find OLE object
                    IOleObject oleObject = (IOleObject) shape;

                    //Get its data and write to file
                    byte[] bytes = oleObject.getData();
                    switch (oleObject.getProgId()) {
                        case "Excel.Sheet.8":
                            String result1 = "output/extractOLEObject.xls";
                            FileOutputStream output1 = new FileOutputStream(result1);
                            output1.write(bytes);
                            output1.close();
                            break;
                        case "AcroExch.Document.11":
                            String result2 = "output/extractOLEObject.pdf";
                            FileOutputStream output2 = new FileOutputStream(result2);
                            output2.write(bytes);
                            output2.close();
                            break;
                        case "Word.Document.12":
                            String result4 = "output/extractOLEObject.docx";
                            FileOutputStream output4 = new FileOutputStream(result4);
                            output4.write(bytes);
                            output4.close();
                            break;
                        case "PowerPoint.Show.8":
                            String result5 = "output/extractOLEObject.ppt";
                            FileOutputStream output5 = new FileOutputStream(result5);
                            output5.write(bytes);
                            output5.close();
                            break;
                        case "PowerPoint.Show.12":
                            String result6 = "output/extractOLEObject.pptx";
                            FileOutputStream output6 = new FileOutputStream(result6);
                            output6.write(bytes);
                            output6.close();
                            break;
                    }
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Extract OLE objects

Thanks for your reading!

Top comments (0)