// Base64 decode it
byte[] bytes = Base64.decodeBase64( xpResult.getBytes("UTF8") );
// Create image part and add it
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, sourcePart, bytes);
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
Inline inline = null;
long cxl = 0;
long cyl = 0;
try {
cxl = Long.parseLong(cx);
cyl = Long.parseLong(cy);
} catch (Exception e) {}
if (cxl==0 || cyl==0) {
// Let BPAI work out size
log.debug("image size - from image");
inline = imagePart.createImageInline( filenameHint, altText,
id1, id2, false);
} else {
// Use existing size
log.debug("image size - from content control size");
// Respect aspect ratio of injected image
ImageSize size = imagePart.getImageInfo().getSize();
double ratio = (double) size.getHeightPx() / (double) size.getWidthPx();
log.debug("fit ratio: " + ratio);
if (ratio > 1) {
cxl = (long)((double) cyl / ratio);
} else {
cyl = (long)((double) cxl * ratio);
}
inline = imagePart.createImageInline( filenameHint, altText,
id1, id2, cxl, cyl, false);
}
// In certain circumstances, save it immediately
if (wmlPackage.getTargetPartStore()!=null
&& wmlPackage.getTargetPartStore() instanceof UnzippedPartStore) {
log.debug("incrementally saving " + imagePart.getPartName().getName());
((UnzippedPartStore)wmlPackage.getTargetPartStore()).saveBinaryPart(imagePart);
// remove it from memory
ByteBuffer bb = null;
imagePart.setBinaryData(bb);//new byte[0]);
imagePart.setImageInfo(null); // this might help as well
}
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();