I am trying to embed a VDX file into a document. I have tried with below sample code. I am not sure if I am trying in the right way. I could see the generated document doesnt have any embedded object . If we rename the doc to a zip extension, we can see the embedded bin object in the folder "\Word\Embeddings\oleObject1.bin". Looks like some relationships are missing here. Could you please help me in getting the OLE object inserted properly in the document ? Thanks in advance for your help.
- Code: Select all
OleObjectBinaryPart oleObj = new OleObjectBinaryPart();
File file = new File("C:/ERD.VDX" );
java.io.InputStream is = new java.io.FileInputStream(file);
oleObj.setBinaryData(is);
String outputfilepath = "C:/OLE.docx";
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.getContentTypeManager().addDefaultContentType("bin", ContentTypes.OFFICEDOCUMENT_OLE_OBJECT);
Relationship relOleObject = wordMLPackage.getMainDocumentPart().addTargetPart(oleObj);
wordMLPackage.save(new java.io.File(outputfilepath));
System.out.println( "Saved output to:" + outputfilepath );
Regards,
Goutham