Page 1 of 1

Unable to put added image to an anchor

PostPosted: Thu Nov 09, 2017 4:55 am
by Alexander Teut
I need to generate an image and put it to a specific position at Word document.

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?

Re: Unable to put added image to an anchor

PostPosted: Thu Nov 09, 2017 6:21 pm
by jason
Can you post an example of the broken docx to this thread?

Re: Unable to put added image to an anchor

PostPosted: Thu Nov 09, 2017 8:06 pm
by Alexander Teut
The files are attached.

The Ok one is generated just by commenting the line

Code: Select all
drawing.getAnchorOrInline().add(anchor);

Re: Unable to put added image to an anchor

PostPosted: Fri Nov 10, 2017 2:29 am
by Alexander Teut
I've found a solution. I generate inline and then copy drawing to anchor.

It looks weird, but it works

Code: Select all
        BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
        Object anchorXml = XmlUtils.unmarshalString(generateXmlAnchor(left, top));
        Anchor anchor = (Anchor) ((JAXBElement)anchorXml).getValue();
        Inline inline = imagePart.createImageInline( filenameHint, altText,
                id1, id2, cx << 4, false);

        anchor.setGraphic(inline.getGraphic());
        org.docx4j.wml.Drawing drawing = factory.createDrawing();
        drawing.getAnchorOrInline().add(anchor);

Re: Unable to put added image to an anchor

PostPosted: Sat Nov 11, 2017 10:43 pm
by jason
In your sample docx, you have your w:drawing at the block level (ie as a sibling to paragraphs, tables).

It should be inline, for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:p >
            <w:r>
                <w:drawing>
                    <wp:inline distT="0" distB="0" distL="0" distR="0">
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Word is just being permissive/forgiving accepting your input at all!

Usually, there'd only be 1 wp:anchor or wp:inline per w:drawing. I haven't checked whether it is legal to have more than one (as you have), or how Word behaves when you do this.