// Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
// Change the rels to TargetMode="External"
// Fetch rels part
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
RelationshipsPart relsPart = documentPart.getRelationshipsPart();
Relationships rels = relsPart.getRelationships();
List<Relationship> relsList = rels.getRelationship();
// For each image rel
for (Relationship r : relsList) {
System.out.println(r.getTargetMode());
if ( r.getType().equals( Namespaces.IMAGE )
&& (r.getTargetMode()==null
|| r.getTargetMode().equalsIgnoreCase("internal"))) {
String target = r.getTarget();
System.out.println("target: " + target);
if (saveImages) {
// Save the image as a file in the specified location
File f = new File(BASE_DIR + "/" + target);
if (f.exists()) {
System.out.println("Overwriting existing object: " + f.getPath() );
} else if ( !f.getParentFile().exists() ) {
System.out.println("creating " + f.getParentFile().getAbsolutePath() );
f.getParentFile().mkdirs();
//f = new File(BASE_DIR + "/" + target);
}
Part p = relsPart.getPart(r);
FileOutputStream fos = new FileOutputStream( f );
((BinaryPart)p).writeDataToOutputStream(fos);
fos.close();
}
// .. externalise - after getPart!
r.setTargetMode("External");
}
}
// Change r:embed to r:link
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
Body body = wmlDocumentEl.getBody();
SingleTraversalUtilVisitorCallback imageVisitor
= new SingleTraversalUtilVisitorCallback(
new TraversalUtilBlipVisitor());