Page 1 of 1

Merging documents with Images

PostPosted: Wed Nov 12, 2014 1:36 am
by unalogica
Hello,
this is my first post, sry for my bad english.
I know this may be a very silly noob question but I've already done some search on the net and here in the forum but i don't know where to turn.

I have to merge n docx document content, taking all the content from 3 different file to generate a new, unique file. I don't need to merge footer/header/comments or other objects, i just need to merge the content (TEXT, TABLES and IMAGES).

What i'm doing at the moment is:

Code: Select all
public static void mergeDocContent(List<File> documents, File outputFile) throws InvalidFormatException, Docx4JException {
       
//Create new document
        WordprocessingMLPackage newDoc = WordprocessingMLPackage.createPackage();

        //Looping the documents
        for (int i = 0; i < documents.size(); i++) {
            //Open current doc
            WordprocessingMLPackage wordMLPackageDoc
                    = WordprocessingMLPackage.load(documents.get(i));
            //Reading the body of the doc
            MainDocumentPart documentPart = wordMLPackageDoc.getMainDocumentPart();

            //Merging the content
            newDoc.getMainDocumentPart().getContent().addAll(documentPart.getContent());
           
        }

        //Saving the new document to a new file
        newDoc.save(outputFile);
    }


Works everything good excepts for images. I get the image block in the generated document with the red X meaning the image cannot be found. I've seen, opening like a ZIP, that the original image is stored as a file in the "media" folder inside the original document. But it's not the same in the generated file. Any help plz :) ?

Re: Merging documents with Images

PostPosted: Fri Nov 14, 2014 7:19 pm
by jason
Each image in a main document part will have a rel ID pointing to the image part.

You'll need to add the image parts to your output docx, and update the rel ids in the output main document part to match the added rels.

Re: Merging documents with Images

PostPosted: Wed Nov 19, 2014 7:45 pm
by unalogica
Tnx for the help.
Is there any quick guide or referecne on how to manipulate the image part of the doc?
Thanks