Using java Syntax Highlighting
public Anchor createImageAnchor
(String filenameHint,
String altText,
int id1,
int id2,
boolean link,
long posHOffset,
long posVOffset
) throws Exception {
WordprocessingMLPackage
wmlPackage = ((WordprocessingMLPackage
) this.
getPackage());
List
<SectionWrapper
> sections
= wmlPackage.
getDocumentModel().
getSections();
PageDimensions page
= sections.
get(sections.
size() - 1
).
getPageDimensions();
CxCy cxcy
= CxCy.
scale(imageInfo, page
);
return createImageAnchor
(filenameHint, altText,
id1, id2, cxcy.
getCx(), cxcy.
getCy(), link, posHOffset, posVOffset
);
}
public Anchor createImageAnchor
(String filenameHint,
String altText,
int id1,
int id2,
long cx,
boolean link,
long posHOffset,
long posVOffset
) throws Exception {
ImageSize size
= imageInfo.
getSize();
Dimension2D dPt
= size.
getDimensionPt();
double imageWidthTwips
= dPt.
getWidth() * 20;
log.
debug("imageWidthTwips: " + imageWidthTwips
);
long cy
;
log.
debug("Scaling image height to retain aspect ratio");
cy
= UnitsOfMeasurement.
twipToEMU(dPt.
getHeight() * 20
* cx
/ imageWidthTwips
);
// Now convert cx to EMU
cx
= UnitsOfMeasurement.
twipToEMU(cx
);
log.
debug("cx=" + cx
+ "; cy=" + cy
);
return createImageAnchor
(filenameHint, altText, id1, id2, cx, cy, link, posHOffset, posVOffset
);
}
public Anchor createImageAnchor
(String filenameHint,
String altText,
int id1,
int id2,
long cx,
long cy,
boolean link,
long posHOffset,
long posVOffset
) throws Exception {
if (filenameHint
== null) {
filenameHint
= "";
}
if (altText
== null) {
altText
= "";
}
String type
;
if (link
) {
type
= "r:link";
} else {
type
= "r:embed";
}
String ml
=
"<wp:anchor distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251658240\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\"" + namespaces
+ ">" // wp14:anchorId=\"28C768E3\" wp14:editId=\"050FA02A\">"
+ "<wp:simplePos x=\"0\" y=\"0\"/>"
+ "<wp:positionH relativeFrom=\"column\"><wp:posOffset>${posHOffset}</wp:posOffset></wp:positionH>"
+ "<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>${posVOffset}</wp:posOffset></wp:positionV>"
+ "<wp:extent cx=\"${cx}\" cy=\"${cy}\"/>"
+ "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>" // b=4445
+ "<wp:wrapNone/>"
+ "<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:extLst><a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\"><a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/></a:ext></a:extLst>"
+ "</a:blip><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>"
// + "<wp14:sizeRelH relativeFrom=\"page\"><wp14:pctWidth>0</wp14:pctWidth></wp14:sizeRelH><wp14:sizeRelV relativeFrom=\"page\"><wp14:pctHeight>0</wp14:pctHeight></wp14:sizeRelV>"
+ "</wp:anchor>";
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", rel.
getId());
mappings.
put("id1",
Integer.
toString(id1
));
mappings.
put("id2",
Integer.
toString(id2
));
Object o
= org.
docx4j.
XmlUtils.
unmarshallFromTemplate(ml, mappings
);
Anchor anchor
= (Anchor
) ((JAXBElement
) o
).
getValue();
return anchor
;
}
Parsed in 0.025 seconds, using
GeSHi 1.0.8.4
are 3 methods modelled on the Inline ones which should do what you want.
Note that this version has <wp:wrapNone/>. If you look at the spec, the "DrawingML - WordprocessingML Drawing" section, you'll see the other options, and what all the attributes do.