In one of the earlier posts, we learned how to programmatically insert the OLE Objects in documents with C#. Today, in this article, we will be embedding PDF and other different documents as OLE objects in Word documents, Excel spreadsheets, PowerPoint presentation slides using Java.
This article will guide you about:
- Java API for OLE Objects
- Insert OLE Objects into MS Word Documents using Java
- Add OLE Objects into Excel Spreadsheets using Java
- Insert Documents into Presentations via OLE using Java
Java API for OLE Objects
Steps and the examples in this article use GroupDocs.Merger for Java to insert documents into other documents via OLE (Object Linking and Embedding). This API also allows us to efficiently combine and split multiple documents with minimum lines of Java code. Before proceeding it will be better if you prepare the environment in any of your relevant ways:
- Download the API from the downloads section.
- For the Maven-based projects, the following is the configuration for your pom.xml
<name>GroupDocs Java API</name>
Insert PDF as OLE Object into MS Word Document using Java
Step and code example below inserts the PDF document into a Word document as an OLE object in Java using the GroupDocs.Merger API.
- Instantiate the Merger object with the source word processing document path or stream.
- Initialize the OleWordProcessingOptions with the path of the PDF document that will be embedded in the Word document.
- Call the importDocument method of the merger class.
- Save the resultant word document by calling the save method.
Insert Word Document as OLE Object into Excel Spreadsheet using Java
Spreadsheets can also embed other documents, like Word documents, spreadsheets, presentations, images or sound clips, etc. Here, I am be adding a Word document into a spreadsheet as an OLE object.
- Initialize the OleSpreadsheetOptions class object by providing the path of the Word document that will be embedded in the spreadsheet.
- Set the options like row and column positions.
- Initialize the Merger class object with the path of the spreadsheet document.
- Call the importDocument method by providing the already set OLE spreadsheet option.
- Save the resultant spreadsheet having the embedded Word document by calling the save method.
Insert Excel Sheet as OLE Object into Presentation using Java
Similarly, if we need to add any external document(s) to our presentations, these can be inserted at the precise location with the few lines of Java code mentioned-below:
- Initialize the OlePresentationOptions class object and pass the path of the spreadsheet document.
- Set the OLE presentation options like x and y coordinates, height and width for the upcoming embedded spreadsheet.
- Instantiate the Merger class object with the presentation document path as parameter.
- Embed the spreadsheet into presentation using the importDocument method of the Merger class.
- Call save method to get the resultant presentation file.
We have learned, how we can programmatically insert OLE objects in Word, Excel, and Powerpoint documents using Java. The major difference while embedding the documents into various kinds of source documents is just the use of the respective OLE Options class. That’s it.