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;
}
}