Scenario:
> Say, I have a docx file named File1.docx conataining images and tables.
> And another docx file named File2.docx conataining images and tables.
> There can be n # of files.
> I try to upload File1.docx .... Filen.docx to an output file.
>
> I iterate through the body of file1 and add its objects to the output
> file body. In the case of the images, I copy the related binary parts
> over and keep track of relationship ids to match. I regenerate a new
> <w:p> object to hold the image. It works if I have only one file with images to copy from.
> For multiple files, each file will call the first image part name as
> image1, then the next as image2 and so on.
>
> To get around, I changed the org.docx4j.openpackaging.parts.Parts class.
> Kept the parts HashMap immutable, just added a getter method to get the map.
> Iterated through it and retrieved all the images in a document at run time.
> There is no way to change the PartName. So I copied the binarycontent
> retrieved from the map and put into a new Part. The code hangs into
> imagePart.setBinaryData(InputStream) method. It can not read the
> characters any more. It is possible that BufferUtil did not do it's
> job right. I am not sure. If you have any idea/suggestion, please share.
>
> Below is a snippet from what I did -
> private void copyBinaryImageParts(WordprocessingMLPackage wordMLPackage)
> throws InvalidFormatException {
>
> Parts documentParts = wordMLPackage.getParts();
> Iterator iterator = documentParts.getParts().entrySet().iterator();
> while (iterator.hasNext()) {
> Map.Entry entry = (Map.Entry) iterator.next();
> if ((entry.getKey() instanceof PartName)
> && ((PartName)
> entry.getKey()).getName().contains("/word/media/image")) {
> BinaryPart part = (BinaryPart) entry.getValue();
>
> Date dt = new Date();
> String partname = "/word/media/image" + dt.getTime() +
> ".png";
> BinaryPart imagePart = new BinaryPart(new
> PartName(partname));
>
>
> imagePart.setBinaryData(BufferUtil.newInputStream(part.getBuffer()));
> imagePart.setContentType(new
> ContentType(ContentTypes.IMAGE_PNG));
> imagePart.setRelationshipType(Namespaces.IMAGE);
> //Add to the arraylist to be used by mark up
>
> rels.add(wmlPack.getMainDocumentPart().addTargetPart(imagePart));
> }
> }
> }
>
>
>