Skip to content
This repository has been archived by the owner on Mar 23, 2020. It is now read-only.

Concatenate/join/append multiple word documents #49

Closed
olivierbarriere opened this issue Nov 27, 2014 · 5 comments
Closed

Concatenate/join/append multiple word documents #49

olivierbarriere opened this issue Nov 27, 2014 · 5 comments

Comments

@olivierbarriere
Copy link

Hi David,

I would really like to be able to concatenate multiple Word documents.
The complete feature is only available in the Docx4j Enterprise Edition and my understanding is that ReporteRs relies on the free open source version.

Alternatively, could you add an option to insert an AltChunk of type docx, and let Word do the processing, as described here:
http://stackoverflow.com/questions/2494549/is-there-any-java-library-maybe-poi-which-allows-to-merge-docx-files

public class MergeDocx {
private static long chunk = 0;
private static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

public void mergeDocx(InputStream s1, InputStream s2, OutputStream os) throws Exception {
    WordprocessingMLPackage target = WordprocessingMLPackage.load(s1);
    insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(s2));
    SaveToZipFile saver = new SaveToZipFile(target);
    saver.save(os);
}

private static void insertDocx(MainDocumentPart main, byte[] bytes) throws Exception {
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + (chunk++) + ".docx"));
        afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setBinaryData(bytes);
        Relationship altChunkRel = main.addTargetPart(afiPart);

        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
        chunk.setId(altChunkRel.getId());

        main.addObject(chunk);
}

}

Thanks and have a nice day,
Olivier

@davidgohel
Copy link
Owner

Dear Olivier

That's an excellent suggestion. I didn't know about Word AltChunk. It works also with html pages.

I will try to implement something named addDocument.
Note that the result can be really messy when mixing 2 different set of styles...

thanks!
David

@davidgohel
Copy link
Owner

Hi,

The new version contains a new function addDocument. See:

?addDocument.docx

You can embed Word documents (docx) into docx objects.

Thanks for the suggestion again.
David

@davidgohel
Copy link
Owner

Hi

Did you had a chance to test the function?

David

@olivierbarriere
Copy link
Author

Hi David,

Yes!!!

My goal was to merge documents with equations and it works pretty well.
Thanks to your addDocument function, I can have a repository of equations
and import them in my report. For the moment, I am perfectly happy with
this workflow, unless you can also provide an "addEquation" function :)
Ideally, we could write a LaTeX code and it would be translated into an
equation object in Word. I am sure that this feature could be very useful
in ReporteRs.
Maybe you could use the MathML trick described in the second answer here
http://tex.stackexchange.com/questions/25223/embed-latex-math-equations-into-microsoft-word
?

I found a minor bug in the addDocument function, the embedded files are not
"released" by rsession.exe and I have to manually "unlock" them to be able to
modify them (with this tool for example: http://www.emptyloop.com/unlocker/)

PS: I wanted to thank you before, but I could not reply to the bug ticket
since you closed it and I could not find a way to contact you directely
either.

Encore un gros merci,
Olivier

@davidgohel
Copy link
Owner

Hi Olivier

Again thanks. I fixed the minor bug. You shouldn't meet that issue anymore with the new version.

If you agree, I'd be happy to go on with the equation discussion - could you send me a short email at <david.gohel at lysis-consultants.fr> so that I get yours?

David

PS: And sorry, I didn't know that closing a ticket had that effect...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants