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

GroupDocs.Merger for Java

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:

  1. Download the API from the downloads section.
  2. For the Maven-based projects, the following is the configuration for your pom.xml
<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-merger</artifactId>
        <version>20.7</version> 
</dependency>

Insert PDF as OLE Object into MS Word Document using Java

Insert PDF in Word Document

Step and code example below inserts the PDF document into a Word document as an OLE object in Java using the GroupDocs.Merger API.

  1. Instantiate the Merger object with the source word processing document path or stream.
  2. Initialize the OleWordProcessingOptions with the path of the PDF document that will be embedded in the Word document.
  3. Call the importDocument method of the merger class.
  4. Save the resultant word document by calling the save method.

Insert Word Document as OLE Object into Excel Spreadsheet using Java

Insert Word File in Excel Spreadsheet

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.

  1. Initialize the OleSpreadsheetOptions class object by providing the path of the Word document that will be embedded in the spreadsheet.
  2. Set the options like row and column positions.
  3. Initialize the Merger class object with the path of the spreadsheet document.
  4. Call the importDocument method by providing the already set OLE spreadsheet option.
  5. Save the resultant spreadsheet having the embedded Word document by calling the save method.

Insert Excel Sheet as OLE Object into Presentation using Java

Insert Excel Sheet in PowerPoint

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:

  1. Initialize the OlePresentationOptions class object and pass the path of the spreadsheet document.
  2. Set the OLE presentation options like x and y coordinates, height and width for the upcoming embedded spreadsheet.
  3. Instantiate the Merger class object with the presentation document path as parameter.
  4. Embed the spreadsheet into presentation using the importDocument method of the Merger class.
  5. Call save method to get the resultant presentation file.

Conclusion

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.

To learn more about the Merger API for Java, visit documentation. In case of any query, GroupDocs Support Team will more than happy to facilitate you at Free Support Forum.

See Also