I'm using the following code (borrowed from ImageAdd example and the following conversation - docx-java-f6/how-to-create-a-floating-image-t1224.html):
- Code: Select all
public static Drawing newImageDraw( WordprocessingMLPackage wordMLPackage,
org.docx4j.wml.ObjectFactory factory,
byte[] bytes,
String filenameHint, String altText,
int id1, int id2, long cx, long cy) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Object anchorXml = XmlUtils.unmarshalString(generateXmlAnchor(4124325, 2847340));
Anchor anchor = (Anchor) ((JAXBElement)anchorXml).getValue();
Inline inline = imagePart.createImageInline( filenameHint, altText,
id1, id2, cx << 4, false);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(inline);
drawing.getAnchorOrInline().add(anchor);
return drawing;
}
private static String generateXmlAnchor(int x, int y){
return generateXmlAnchor(x, y, generateAnchorId());
}
private static String generateXmlAnchor(int x, int y, int id) {
String xmlAnchor = ""
+ "<wp:anchor xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251659264\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
+ " <wp:simplePos x=\"0\" y=\"0\"/>"
+ " <wp:positionH relativeFrom=\"page\">"
+ " <wp:posOffset>" + x + "</wp:posOffset>"
+ " </wp:positionH>"
+ " <wp:positionV relativeFrom=\"page\">"
+ " <wp:posOffset>" + y + "</wp:posOffset>"
+ " </wp:positionV>"
+ " <wp:extent cx=\"130629\" cy=\"130629\"/>"
+ " <wp:effectExtent l=\"0\" t=\"0\" r=\"22225\" b=\"22225\"/>"
+ " <wp:wrapNone/>"
+ " <wp:docPr id=\"" + id + "\" name=\"" + id +" Elipse\"/>"
+ " <wp:cNvGraphicFramePr/>"
+ " <a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
+ " <a:graphicData uri=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">"
+ " <wps:wsp>"
+ " <wps:cNvSpPr/>"
+ " <wps:spPr>"
+ " <a:xfrm>"
+ " <a:off x=\"0\" y=\"0\"/>"
+ " <a:ext cx=\"130629\" cy=\"130629\"/>"
+ " </a:xfrm>"
+ " <a:prstGeom prst=\"ellipse\">"
+ " <a:avLst/>"
+ " </a:prstGeom>"
+ " </wps:spPr>"
+ " <wps:style>"
+ " <a:lnRef idx=\"2\">"
+ " <a:schemeClr val=\"accent1\">"
+ " <a:shade val=\"50000\"/>"
+ " </a:schemeClr>"
+ " </a:lnRef>"
+ " <a:fillRef idx=\"1\">"
+ " <a:schemeClr val=\"accent1\"/>"
+ " </a:fillRef>"
+ " <a:effectRef idx=\"0\">"
+ " <a:schemeClr val=\"accent1\"/>"
+ " </a:effectRef>"
+ " <a:fontRef idx=\"minor\">"
+ " <a:schemeClr val=\"lt1\"/>"
+ " </a:fontRef>"
+ " </wps:style>"
+ " <wps:bodyPr rot=\"0\" spcFirstLastPara=\"0\" vertOverflow=\"overflow\" horzOverflow=\"overflow\" vert=\"horz\" wrap=\"square\" lIns=\"91440\" tIns=\"45720\" rIns=\"91440\" bIns=\"45720\" numCol=\"1\" spcCol=\"0\" rtlCol=\"0\" fromWordArt=\"0\" anchor=\"ctr\" anchorCtr=\"0\" forceAA=\"0\" compatLnSpc=\"1\">"
+ " <a:prstTxWarp prst=\"textNoShape\">"
+ " <a:avLst/>"
+ " </a:prstTxWarp>"
+ " <a:noAutofit/>"
+ " </wps:bodyPr>"
+ " </wps:wsp>"
+ " </a:graphicData>"
+ " </a:graphic>"
+ " <wp14:sizeRelH relativeFrom=\"margin\">"
+ " <wp14:pctWidth>0</wp14:pctWidth>"
+ " </wp14:sizeRelH>"
+ " <wp14:sizeRelV relativeFrom=\"margin\">"
+ " <wp14:pctHeight>0</wp14:pctHeight>"
+ " </wp14:sizeRelV>"
+ "</wp:anchor>";
return xmlAnchor;
}
Sadly, it doesn't work. The result document can't be opened by ms word. When I comment drawing.getAnchorOrInline().add(anchor); it works fine, but of course there's no anchor anymore.
I can't find any working example for an image anchored in a specified place using latest versions of docx4j. So, I still don't see any way to connect my generated image with Anchor. When I produce the document, it has broken XML state and nothing more.
Is there any working code that generated image and inserts it with anchor?