I know how to add embedded image as following. Could some one tell me how to add linked image?
File file = new File(fileurl);
java.io.InputStream is = new java.io.FileInputStream(file );
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline;
if (cx == 0) {
inline = imagePart.createImageInline(filenameHint, altText, id1, id2);
} else {
inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx);
}
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.R run = factory.createR();
p.getParagraphContent().add(run);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getRunContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
wordMLPackage.getMainDocumentPart().addObject(p);