How to Create a Tight Wrapped Anchored Image
Posted: Fri Sep 18, 2015 2:09 am
I read through pages of white papers online and can't find a straight answer as to how to create a tight wrapped anchored image. I have tried a few solutions that got me close to what I need but I cannot get a full solution. Here's the code that I currently have....
Using a solution I found here http://www.docx4java.org/forums/docx-java-f6/inline-vs-anchor-images-t726.html by tstirrat ยป Sat Sep 29, 2012 2:29 pm
This solution actually works but it sets the picture to the top right of the page as a floating image in front of text. But what I want to do is create a tight wrapped image (see enclosed sample.docx) so that text can flow around it and not behind/in front of the image. I tried changing anchor.setWrapNone(wordDmlFactory.createCTWrapNone()); to anchor.setWrapTight(wordDmlFactory.createCTWrapTight()); but the document opens with an error. I am fairly new to docx4j and have read through (as best I can) the various documents available online. But I am still unable to get the desired solution. Can anyone please help me get this accomplished? Thanks.
Using a solution I found here http://www.docx4java.org/forums/docx-java-f6/inline-vs-anchor-images-t726.html by tstirrat ยป Sat Sep 29, 2012 2:29 pm
- Code: Select all
public static void createAnchor(org.docx4j.openpackaging.packages.WordprocessingMLPackage wordMLPackage, org.docx4j.wml.ObjectFactory factory, byte[] bytes, String filenameHint, String altText, int id1, int id2) throws Exception {
org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
org.docx4j.dml.wordprocessingDrawing.Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
// convert the inline to an anchor (xml contents are essentially the same)
String anchorXml = XmlUtils.marshaltoString(inline, true, false, Context.jc, Namespaces.NS_WORD12, "anchor", Inline.class);
org.docx4j.dml.ObjectFactory dmlFactory = new org.docx4j.dml.ObjectFactory();
org.docx4j.dml.wordprocessingDrawing.ObjectFactory wordDmlFactory = new org.docx4j.dml.wordprocessingDrawing.ObjectFactory();
Anchor anchor = (Anchor) XmlUtils.unmarshalString(anchorXml, Context.jc, Anchor.class);
anchor.setSimplePos(dmlFactory.createCTPoint2D());
anchor.getSimplePos().setX(0L);
anchor.getSimplePos().setY(0L);
anchor.setSimplePosAttr(false);
anchor.setPositionH(wordDmlFactory.createCTPosH());
anchor.getPositionH().setAlign(STAlignH.RIGHT);
anchor.getPositionH().setRelativeFrom(STRelFromH.MARGIN);
anchor.setPositionV(wordDmlFactory.createCTPosV());
anchor.getPositionV().setPosOffset(0);
anchor.getPositionV().setRelativeFrom(STRelFromV.PAGE);
anchor.setWrapNone(wordDmlFactory.createCTWrapNone());
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.Drawing drawing = getFactory().createDrawing();
R run = getFactory().createR();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(anchor);
org.docx4j.wml.P p = factory.createP();
p.getContent().add(run);
wordMLPackage.getMainDocumentPart().addObject(p);
}
This solution actually works but it sets the picture to the top right of the page as a floating image in front of text. But what I want to do is create a tight wrapped image (see enclosed sample.docx) so that text can flow around it and not behind/in front of the image. I tried changing anchor.setWrapNone(wordDmlFactory.createCTWrapNone()); to anchor.setWrapTight(wordDmlFactory.createCTWrapTight()); but the document opens with an error. I am fairly new to docx4j and have read through (as best I can) the various documents available online. But I am still unable to get the desired solution. Can anyone please help me get this accomplished? Thanks.