/** * Create a <wp:inline> element suitable for this image, which can be linked or embedded in * w:p/w:r/w:drawing, specifying height and width. Note that you'd ordinarily use one of the * methods which don't require you to specify height (cy). * * @param filenameHint Any text, for example the original filename * @param altText Like HTML's alt text * @param id1 An id unique in the document * @param id2 Another id unique in the document None of these things seem to be exposed in Word * 2007's user interface, but Word won't open the document if any of the attributes these go * in (except @ desc) aren't present! * @param cx Image width in EMU * @param cy Image height in EMU * @param link true if this is to be linked not embedded * @throws Exception */ public Inline createImageInline( String filenameHint, String altText, int id1, int id2, long cx, long cy, boolean link) throws Exception { if (filenameHint == null) { filenameHint = ""; } if (altText == null) { altText = ""; } String type; if (link) { type = "r:link"; } else { type = "r:embed"; } String ml = // "<w:p ><w:r>" + // "<w:drawing>" + "<wp:inline distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\"" + namespaces + ">" + "<wp:extent cx=\"${cx}\" cy=\"${cy}\"/>" + "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>" + // l=\"19050\" "<wp:docPr id=\"${id1}\" name=\"${filenameHint}\" descr=\"${altText}\"/><wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/></wp:cNvGraphicFramePr><a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + "<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\"><pic:nvPicPr><pic:cNvPr id=\"${id2}\" name=\"${filenameHint}\"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill>" + "<a:blip " + type + "=\"${rEmbedId}\"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill>" + "<pic:spPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"${cx}\" cy=\"${cy}\"/></a:xfrm><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic>" + "</wp:inline>"; // + // "</w:drawing>" + // "</w:r></w:p>"; java.util.HashMap<String, String> mappings = new java.util.HashMap<String, String>(); mappings.put("cx", Long.toString(cx)); mappings.put("cy", Long.toString(cy)); mappings.put("filenameHint", filenameHint); mappings.put("altText", altText); mappings.put("rEmbedId", this.getRelLast().getId()); mappings.put("id1", Integer.toString(id1)); mappings.put("id2", Integer.toString(id2)); Object o = org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings); Inline inline = (Inline) ((JAXBElement) o).getValue(); return inline; }