Page 1 of 1

docx4j:merge documents having images in table column

PostPosted: Fri Dec 02, 2016 2:36 am
by venkateshg
ImageMergedocuments.docx
by using above code i got the following output.
(55.78 KiB) Downloaded 306 times
SampleImage0002.docx
sample input document1
(23.95 KiB) Downloaded 279 times
sampleImage00001.docx
sample input document2
(65.8 KiB) Downloaded 292 times
Hi
I am having two word documents.In first document i have two images and in the second document table has images in entire first column.i want to merge these two documents.I have tried with sample code but the result are not getting as i am expected.I am attaching the sample documents and the sample out put what i have got.
Please help me.

Thanks in advance.

Code: Select all
}
   
public static WordprocessingMLPackage mergeDocFiles(List<String> strFiles) {
      
   try {
      File first = new File(strFiles.get(0));
      WordprocessingMLPackage f = WordprocessingMLPackage.load(first);
      
      Br objBr = new Br();
      org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
      for(int i=1; i<strFiles.size(); i++){
         String strFilePath = strFiles.get(i);
         File objFile = new File(strFilePath);
         WordprocessingMLPackage content = WordprocessingMLPackage.load(objFile);
         List body = content.getMainDocumentPart().getJAXBNodesViaXPath(
                        "//w:body", false);
         for (Object b : body) {
            List filhos = ((org.docx4j.wml.Body) b).getContent();
            for (Object k : filhos)
               f.getMainDocumentPart().addObject(k);
            
            
            objBr.setType(STBrType.PAGE);
            P para = factory.createP();
            para.getParagraphContent().add(objBr);
         }
         List blips = content.getMainDocumentPart().getJAXBNodesViaXPath(
               "//a:blip", false);
         List<Relationship> rels=new ArrayList<Relationship>();
         List<Relationship> newrels=new ArrayList<Relationship>();
         for(Object el : blips){
              try {
                 
                  CTBlip blip = (CTBlip) el;

                  RelationshipsPart parts = content.getMainDocumentPart().getRelationshipsPart();
                  Relationship rel = parts.getRelationshipByID(blip.getEmbed());

                  RelationshipsPart docRels = f.getMainDocumentPart().getRelationshipsPart();
                   rels = docRels.getJaxbElement().getRelationship();
                   //newrels=new ArrayList<Relationship>();
                  //newrels.addAll(rels);
                 
                 
                  docRels.removeRelationship(rel);
                  rel.setId(null);
                  docRels.addRelationship(rel);
                  blip.setEmbed(rel.getId());

                  f.getMainDocumentPart().addTargetPart(content.getParts().getParts().get(new PartName("/word/"+rel.getTarget())));
                 
              }
             
             
              catch (Exception ex){}
          }
         
      }
      
      return f;

   } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
   }

}

Re: docx4j:merge documents having images in table column

PostPosted: Sun Dec 04, 2016 3:58 pm
by jason
A quick look at your code suggests 2 basic problems/conceptual errors.

First, when you copy a rel from one WordprocessingMLPackage package to another, you'll typically need to change the rel ID, to avoid collision with existing ID.

Second, you need to think about the rel's target part. It is still attached to the source package.

You can use the addTargetPart method to avoid these issues, or even higher level, BinaryPartAbstractImage (see ImageAdd sample).

Note that in the general case, merging/concatenating Word documents is quite complex. That's why Plutext offers MergeDocx as part of Docx4j Enterprise.