Page 1 of 1

Implementing ShapeProperties for images in docx

PostPosted: Wed Mar 13, 2019 1:31 am
by Madhuri92
Hello,

How can I set CTShapeProperties (of DML package) to the image/picture that I am trying to insert in docx using docx4j? Is it even possible to use DML classes while creating docx using docx4j?

Thanks

Re: Implementing ShapeProperties for images in docx

PostPosted: Wed Mar 13, 2019 8:09 am
by jason
This is straightforward .. pls attach a suitable sample docx and I will help

Re: Implementing ShapeProperties for images in docx

PostPosted: Wed Mar 13, 2019 3:07 pm
by Madhuri92
Hello Jason,

Thank you for replying. My current code for creating a document with an image looks as following:

public void addPicture(String path, float X, float Y, float width, float height) throws Exception {
File file = new File(path);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(this.getWordprocessingMLPackage(), file);
Relationship rel = this.getWordprocessingMLPackage().getMainDocumentPart().addTargetPart(imagePart);

Map<String, String> styleMap = new HashMap<String, String>();
styleMap.put("position", "absolute"); styleMap.put("margin-left", (X +
"pt")); styleMap.put("margin-top", (Y + "pt")); styleMap.put("width", (width
+ "pt")); styleMap.put("height", height + "pt");
styleMap.put("mso-position-horizontal","absolute");
styleMap.put("mso-position-vertical","absolute");
styleMap.put("mso-position-horizontal-relative", "page");
styleMap.put("mso-position-vertical-relative", "page");
styleMap.put("visibility", "visible");
styleMap.put("mso-wrap-style","square");

Pict pict = createImageInline(rel, mapToString(":", ";", styleMap), "title");
JAXBElement<org.docx4j.wml.Pict> pictWrapped = wmlObjectFactory.createRPict(pict); ShapeImpl shape = new ShapeImpl();
shape.setStyle(mapToString(":", ";", styleMap));
shape.getEGShapeElements().add(pictWrapped);
JAXBElement<org.docx4j.vml.CTShape> shapeWrapped =
vmlObjectFactory.createShape(shape); // range.getContent().add(shapeWrapped);
this.getContent().add(shapeWrapped);
}

Above code works correctly to render an image. However, I want to set other image properties such as FlipH, FlipV and Rotation through code which is possible using CTTransform2D and CTShapeProperties, however I am not sure how to use them with above code.

Re: Implementing ShapeProperties for images in docx

PostPosted: Wed Mar 13, 2019 3:31 pm
by jason
Again, please post a sample docx which uses the XML you'd like to create.

Re: Implementing ShapeProperties for images in docx

PostPosted: Thu Apr 04, 2019 5:25 pm
by Madhuri92
Hello Jason,

Thank you for mentioning about xml sample.

What I did was, Created one docx file, inserted one image and applied rotation property. After this converted this file into xml file. Now I could see the various properties those were set for this rotated image.

I simply had to set rotation property in style parameter. So in above mentioned java code, I just had to set styleMap.put("rotation", "30"); and this would rotate my image to 30 degree through code.

And with this my problem is fixed.