Page 1 of 1

How to create a floating image

PostPosted: Fri Oct 19, 2012 8:16 am
by mgrela
Hi,

how to create a floating (as opposed to an inline) image with docx4j ? After loading the image as a binary part:

BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, source_part, new File(filename));

the only function which can be called on that is createImageInline, which produces the image wrapped in an <inline> element not the <anchor> element like I would like it to be. Is it possible ?

Re: How to create a floating image

PostPosted: Fri Oct 19, 2012 7:34 pm
by jason
For clarity, could you please attach a docx containing just a floating image (ie produced using Word or perhaps OpenOffice)?

Re: How to create a floating image

PostPosted: Sat Oct 20, 2012 12:52 am
by mgrela
Here you go. I'd like to get this result - I can move the image around without it being constrained by the text flow.

Re: How to create a floating image

PostPosted: Tue Oct 23, 2012 5:37 pm
by jason
Syntax: [ Download ] [ Hide ]
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.023 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.

Re: How to create a floating image

PostPosted: Sat Oct 27, 2012 4:02 am
by mgrela
Thanks, I will test.

Re: How to create a floating image

PostPosted: Wed Jul 02, 2014 7:59 pm
by meeran29
wat is imageInfo ? Where have you declared it?

Re: How to create a floating image

PostPosted: Fri Jul 04, 2014 10:04 am
by jason
You should read https://github.com/plutext/docx4j/blob/ ... Image.java

There is now also available to docx4j webapp (linked in menu above) which can generate for you code matching whatever floating/wrapping options you have selected in your sample docx.

Re: How to create a floating image

PostPosted: Sat Dec 13, 2014 9:55 pm
by roded
Hi,
I'm not clear on something: the above mentioned methods are commented out since Nov, 2012.
https://github.com/plutext/docx4j/commi ... c27836495c
Is there a reason for this?
Thanks