Page 1 of 1

Merge two docx file

PostPosted: Sat Jan 24, 2009 6:16 pm
by twentyseven
Hello,

Is it possible to merge two doc file.
In fact I use the merge variable example, but I would like to merge a document with a list of map.
My idea was to merge docx after docx and then merge the result docx.

Thank's.

Re: Merge two docx file

PostPosted: Tue Jan 27, 2009 3:57 am
by jason
Yes, it is quite possible to merge 2 docx documents, but there are some details you need to handle.

When you say "list of map", do you mean images? If you have images in each of the 2 documents, the chances are that you'll have to renumber them so they have distinct @r:embed and matching entries in the document relationships part. I think there is an earlier post on this topic somewhere ...

(Same thing applies to hyperlinks, footnotes/endnotes etc)

Also, if the 2 documents have different styles with the same name, you'll need to decide which style to use.

cheers,

Jason

Re: Merge two docx file

PostPosted: Tue Jan 27, 2009 8:15 am
by twentyseven
When I talk about List of Map, this is not images but Map of variables.
In fact i have a docx file that I use as template and that contains variables ${...}.

Then I'd like to merge my template with several set of variables, for example to print a set of letters or something like that.

I would have an utils method that take as parameters a List of Map (List<HasMap<String, String>>).
And so I'd like to merge the docx files during the Java merge, but I think it's not as simple than merge the doc part.

Thank's

Re: Merge two docx file

PostPosted: Wed Jan 28, 2009 3:05 am
by jason
When I talk about List of Map, this is not images but Map of variables.
In fact i have a docx file that I use as template and that contains variables ${...}.

Then I'd like to merge my template with several set of variables, for example to print a set of letters or something like that.

I would have an utils method that take as parameters a List of Map (List<HasMap<String, String>>).


ok, now i understand :)

And so I'd like to merge the docx files during the Java merge, but I think it's not as simple than merge the doc part.


See my comments above. If the paragraphs are simple (ie no images, hyperlinks, comments etc) then it is quite easy. You could use something like:

Code: Select all
      List <Object> bodyChildren = getBody(wordMLPackage1).getEGBlockLevelElts();
                getBody(wordMLPackage2).getEGBlockLevelElts().addAll(bodyChildren);

private Body getBody( WordprocessingMLPackage wordMLPackage ) {

      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
      return  wmlDocumentEl.getBody();
}


As I mentioned before, things are a bit more complicated if the docs use different styles.

You could manipulate the parts, or possibly use Context Free Chunks: see http://blogs.msdn.com/brian_jones/archive/2005/07/20/441167.aspx

I'd suggest you use the latest build http://dev.plutext.org/docx4j/docx4j-nightly-20090128.tar.gz

Re: Merge two docx file

PostPosted: Sun Nov 14, 2010 8:07 pm
by jason
I've created a paid extension for docx4j which can merge docx properly.

See http://dev.plutext.org/blog/2010/11/mer ... documents/ for details.

You can buy it at www.plutext.com. Purchases of this extension support the further development of docx4j.