I need to create an image whose position is floating. Using an adaptation of the solution provided here (below) I produced a document (attached). For reasons I have been unable to understand, the image does not appear in the document. I get a drawing of the correct size and position (visible when I Select All in the document), but the image does not appear. The relationship ID is correct and the image part is in the package. What do I have wrong here? I have inspected the main document XML and I don't see anything off when compared to a doc I created by hand.
Using java Syntax Highlighting
public Anchor createImageAnchor(String filenameHint, String altText,
int id1, int id2, long posHOffset, long posVOffset, ImageInfo imageInfo, String relId) throws Exception {
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();
BinaryPartAbstractImage.CxCy cxcy = BinaryPartAbstractImage.CxCy.scale(imageInfo, page);
return createImageAnchor(filenameHint, altText, id1, id2, cxcy.getCx(), posHOffset, posVOffset, imageInfo, relId);
}
public Anchor createImageAnchor(String filenameHint, String altText, int id1, int id2,
long cx, long posHOffset, long posVOffset, ImageInfo imageInfo, String relId) throws Exception {
ImageSize size = imageInfo.getSize();
Dimension2D dPt = size.getDimensionPt();
double imageWidthTwips = dPt.getWidth() * 20;
long cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20 * cx / imageWidthTwips);
cx = UnitsOfMeasurement.twipToEMU(cx);
return createImageAnchor(filenameHint, altText, id1, id2, cx, cy, posHOffset, posVOffset, relId);
}
public Anchor createImageAnchor(String filenameHint, String altText,
int id1, int id2, long cx, long cy,
long posHOffset, long posVOffset, String relId) throws Exception {
if (filenameHint == null) {
filenameHint = "";
}
if (altText == null) {
altText = "";
}
cx = 2170500;
cy = 2170500;
String ml =
"<wp:anchor distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251658240\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">" // TODO do we need these? namespaces + ">" // wp14:anchorId=\"28C768E3\" wp14:editId=\"050FA02A\">"
+ "<wp:simplePos x=\"0\" y=\"0\"/>"
+ "<wp:positionH relativeFrom=\"column\"><wp:posOffset>0</wp:posOffset></wp:positionH>"
+ "<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>0</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.microsoft.com/office/word/2010/wordprocessingGroup\">"
+ " <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 xmlns:r=\"http://schemas.openxmlformats.org/drawingml/2006/main\" r:embed=\"${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<>();
mappings.put("cx", Long.toString(cx));
mappings.put("cy", Long.toString(cy));
mappings.put("filenameHint", filenameHint);
mappings.put("altText", altText);
mappings.put("rEmbedId", relId);
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;
int id1, int id2, long posHOffset, long posVOffset, ImageInfo imageInfo, String relId) throws Exception {
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();
BinaryPartAbstractImage.CxCy cxcy = BinaryPartAbstractImage.CxCy.scale(imageInfo, page);
return createImageAnchor(filenameHint, altText, id1, id2, cxcy.getCx(), posHOffset, posVOffset, imageInfo, relId);
}
public Anchor createImageAnchor(String filenameHint, String altText, int id1, int id2,
long cx, long posHOffset, long posVOffset, ImageInfo imageInfo, String relId) throws Exception {
ImageSize size = imageInfo.getSize();
Dimension2D dPt = size.getDimensionPt();
double imageWidthTwips = dPt.getWidth() * 20;
long cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20 * cx / imageWidthTwips);
cx = UnitsOfMeasurement.twipToEMU(cx);
return createImageAnchor(filenameHint, altText, id1, id2, cx, cy, posHOffset, posVOffset, relId);
}
public Anchor createImageAnchor(String filenameHint, String altText,
int id1, int id2, long cx, long cy,
long posHOffset, long posVOffset, String relId) throws Exception {
if (filenameHint == null) {
filenameHint = "";
}
if (altText == null) {
altText = "";
}
cx = 2170500;
cy = 2170500;
String ml =
"<wp:anchor distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251658240\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">" // TODO do we need these? namespaces + ">" // wp14:anchorId=\"28C768E3\" wp14:editId=\"050FA02A\">"
+ "<wp:simplePos x=\"0\" y=\"0\"/>"
+ "<wp:positionH relativeFrom=\"column\"><wp:posOffset>0</wp:posOffset></wp:positionH>"
+ "<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>0</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.microsoft.com/office/word/2010/wordprocessingGroup\">"
+ " <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 xmlns:r=\"http://schemas.openxmlformats.org/drawingml/2006/main\" r:embed=\"${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<>();
mappings.put("cx", Long.toString(cx));
mappings.put("cy", Long.toString(cy));
mappings.put("filenameHint", filenameHint);
mappings.put("altText", altText);
mappings.put("rEmbedId", relId);
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.024 seconds, using GeSHi 1.0.8.4
I then add the anchor to a run inside a paragraph in the main document part like so:
Using java Syntax Highlighting
P p = factory.createP();
R run = factory.createR();
Drawing drawing = factory.createDrawing();
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(this.wordMLPackage, imageArray);
Anchor anchor = createImageAnchor("", "", 123, 456, 0, 0, imagePart.getImageInfo(), imagePart.getRelLast().getId());
drawing.getAnchorOrInline().add(anchor);
run.getContent().add(drawing);
p.getContent().add(run);
this.wordMLPackage.getMainDocumentPart().addObject(p);
R run = factory.createR();
Drawing drawing = factory.createDrawing();
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(this.wordMLPackage, imageArray);
Anchor anchor = createImageAnchor("", "", 123, 456, 0, 0, imagePart.getImageInfo(), imagePart.getRelLast().getId());
drawing.getAnchorOrInline().add(anchor);
run.getContent().add(drawing);
p.getContent().add(run);
this.wordMLPackage.getMainDocumentPart().addObject(p);
Parsed in 0.015 seconds, using GeSHi 1.0.8.4