Page 1 of 1

Embedding OLE object in Document

PostPosted: Thu Jul 17, 2014 8:02 pm
by Goutham Reddy
Hi All,

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

Re: Embedding OLE object in Document

PostPosted: Thu Jul 17, 2014 8:54 pm
by jason
Plutext's Enterprise Edition of docx4j contains code for embedding OLE objects.

However, Office documents generally aren't embedded in the normal way.

If you embed a Visio VDX in a docx using Word, and attach the result in this thread, I'll take a look at it for you.

Re: Embedding OLE object in Document

PostPosted: Thu Jul 17, 2014 10:03 pm
by Goutham Reddy
Hi Jason,

Thank you very much for your reply. Attached is the docx file embedded with VDX file. However, it is not working for PDF and image files too. Could you please share the code to embed PDF/Image file as well?

Thanks in advance for your help.


Regards,
Goutham

Re: Embedding OLE object in Document

PostPosted: Mon Jul 21, 2014 8:18 pm
by Goutham Reddy
Hi Jason,

After reading your comments in the previous reply ("Plutext's Enterprise Edition of docx4j contains code for embedding OLE objects"), I have tried with downloading enterprise edition and it worked fine. However, I would like to know if this can be supported in docx4j non-enterprise (free/simple) edition ?

Code: Select all
   boolean OLE_LINK_ONLY = false;
      
      ////////////////////////////////////////////////////////
      // For this sample, create a docx package using docx4j

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

      
      ////////////////////////////////////////////////////////
      // OLE config

      EmbeddingType embeddingType = EmbeddingType.TXT; // CSV is a text format
      byte[] contentBytes = FileUtils.readFileToByteArray(new File(System.getProperty("user.dir")+"/Hi.VDX" ));
      
      // the image on the document surface
      byte[] imageBytes = FileUtils.readFileToByteArray(new File(System.getProperty("user.dir")+"/greentick.png" ));
      
      // if you know your image mime type, you can avoid image introspection (which avoids temp file creation)
      String mime = "image/png"; // .. otherwise, set this to null
      
      // position in docx content list
      int index = -1; // place at end
            
      // Both link and embed require this
        String filepath = "C:\\Users\\jharrop\\Documents\\Hi.VDX";     // typically the original file location
      String caption = "Hi.VDX"; // can be anything
      String command ="C:\\Users\\jharrop\\AppData\\Local\\Temp\\Hi.VDX"; // a temp file location
      
        // For link only
      String targetURL = "file:///" + command;

      
      ////////////////////////////////////////////////////////
      // Perform OLE
      
      OleHelperDocx OleHelperDocx = new OleHelperDocx(wordMLPackage);
      if (OLE_LINK_ONLY) {
         
         OleHelperDocx.link(index, embeddingType, caption, filepath, command, targetURL, imageBytes);

          // Save the resulting docx
         wordMLPackage.save(new File(System.getProperty("user.dir")
               + "/OUT_MSG_linked.docx"));
         
      } else {
         
         OleHelperDocx.embed(index, embeddingType,
               caption, filepath, command,
               contentBytes, imageBytes);

          // Save the resulting docx
         wordMLPackage.save(new File(System.getProperty("user.dir")
               + "/OUT_MSG_embedded.docx"));
         
      }
      


Thanks,
Goutham Reddy

Re: Embedding OLE object in Document

PostPosted: Fri Aug 15, 2014 9:21 pm
by jason
Goutham Reddy wrote:I would like to know if this can be supported in docx4j non-enterprise (free/simple) edition ?


In principle there is nothing to stop you writing code which uses docx4j to do this, but Plutext has chosen to package the code it developed to do it in the Enterprise version only. Enterprise sales are Plutext's main source of income, and it is that income which supports the continued development of docx4j, for the benefit of all.

If you find the Enterprise edition unaffordable, and have special circumstances, you are welcome to make your case for a discount.

Trust this makes sense .. Jason