Page 1 of 1

Merging documents with OLEObjects

PostPosted: Tue Jul 28, 2009 7:07 pm
by jbeltran
Hi,

I'm trying to merge two documents into a new document. One of the documents has an embedded Excel spreadsheet as an OLEObject. If I remove the OLEObject, I can merge the files into a new file with no issues.

That being said, is there an sample file in the source code that illustrates this use case? Is it possible to do this in docx4j? If so, what specific code should I be using to do this?

I know that that OLEObject has a corresponding relationship defined in document.xml.rels. I'm assuming I have to transfer that relationship to the new document's document.xml.rels file, but I also notice there's a corresponding *.xls file in the package. Programatically is there API way for copying that actual file into the new document I'm merging into?

Thanks in advance!
Justin

Re: Merging documents with OLEObjects

PostPosted: Mon Aug 03, 2009 11:24 am
by jason
There are three things you need in general:
1. the correct xml in the Main Document Part (document.xml); you can pretty much copy this from the old document; you'll need to correct any rel id.
2. the OLE object itself (the .xls part).
3. the relationship connecting the two together.

I think you've already worked out how to do 2 & 3? You create the part, then use addTargetPart.

I haven't looked specifically at Excel OLE objects and how they are represented. We have org.docx4j.openpackaging.parts.WordprocessingML.OleObjectBinaryPart for representing OLE blobs, which may or may not be of use to you.

Let me know how you go; if you run into difficulties, pls post your code and the source Word docx (containing the Excel OLE object), saved as .xml from Word.

Re: Merging documents with OLEObjects

PostPosted: Thu Sep 10, 2009 4:59 pm
by eford
I'm coming up against the same problem Justin described:

I know that that OLEObject has a corresponding relationship defined in document.xml.rels. I'm assuming I have to transfer that relationship to the new document's document.xml.rels file, but I also notice there's a corresponding *.xls file in the package. Programatically is there API way for copying that actual file into the new document I'm merging into?


That is, I haven't figured out how to actually embed the xls file into the package. Examining a package created in Word shows the spreadsheet is added to an "Embeddings" folder within the package. I haven't tried it yet but my guess would be I'll have to use an approach similar to that used for adding image files.

Re: Merging documents with OLEObjects

PostPosted: Fri Sep 11, 2009 2:40 am
by jason
jbeltran wrote:Programatically is there API way for copying that actual file into the new document I'm merging into?


http://dev.plutext.org/trac/docx4j/brow ... nPart.java contains:

Code: Select all
   public static void attachForeignPart( WordprocessingMLPackage wordMLPackage,
         Base attachmentPoint,
         ContentTypeManager foreignCtm,
         String resolvedPartUri, InputStream is) throws Exception{
      
      
      Part foreignPart = Load.getRawPart(is, foreignCtm,  resolvedPartUri);
      attachmentPoint.addTargetPart(foreignPart);
      // Add content type
      ContentTypeManager packageCtm = wordMLPackage.getContentTypeManager();
      packageCtm.addOverrideContentType(foreignPart.getPartName().getURI(), foreignPart.getContentType());
      
      System.out.println("Attached foreign part: " + resolvedPartUri);
      
   }



Note that addTargetPart returns the new rel, which you'll need to embed in the main document part

Microsoft has published a couple of blog posts you might find helpful:


Eric, it would be good if you could produce a sample of embedding a spreadsheet, which I could add to the samples. If you need further help, just post the code you have so far.

cheers .. Jason