1

I need to add below shapes available in MS word using Apache poi library.

I am not able to find any specific class/method available for word to add such shapes.

However, I can find some method to add shapes in excel from apache.

 XSSFDrawing patriarch= (XSSFDrawing) sheet3.createDrawingPatriarch();
 XSSFClientAnchor regionr = patriarch.createAnchor(0, 0, 1, 1, (short)6, 12, (short)8.95, 12);
 XSSFSimpleShape region1Shapevr = patriarch.createSimpleShape(regionr);
 region1Shapevr.setShapeType(ShapeTypes.LINE); 

Can you please suggest if there is any similar class/method available or other approach to add shapes as well in word document.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Arvind
  • 46
  • 9
  • Hi @Arvind, did you tried [this](https://stackoverflow.com/questions/40302326/apache-poi-xwpf-adding-shapes-to-header) – Sridhar Dec 17 '18 at 11:54
  • Yes Sridhar, I have tried it but it din't work. I want to add ShapeType.CALLOUT_1 https://poi.apache.org/apidocs/dev/org/apache/poi/sl/usermodel/ShapeType.html – Arvind Dec 17 '18 at 12:19
  • share the code snippet you tried with, so that we can try to fix from there. – Sridhar Dec 17 '18 at 12:24
  • for (XWPFParagraph p : doc.getParagraphs()) { List runs = p.getRuns(); for (XWPFRun r : runs) { CTGroup ctGroup = CTGroup.Factory.newInstance(); CTShape ctShape = ctGroup.addNewShape(); ctGroup = CTGroup.Factory.newInstance(); ctShape = ctGroup.addNewShape(); ctShape.setStyle("width:80pt;height:24pt"); ctShape.setStyle("width:80pt;height:24pt"); Node ctGroupNode = ctGroup.getDomNode(); CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode); CTR cTR = r.getCTR(); cTR.addNewPict(); cTR.setPictArray(0, ctPicture); r.addTab(); }}} – Arvind Dec 17 '18 at 12:34
  • @Seelenvirtuose: This is not a duplicate but instead it is to broad to answer. My example is only for creating text box shapes but not for creating shapes in general. Creating general `Word` shapes is not supported by neither the low level `ooxml-schemas` nor `apache poi`. It would only be possible by manipulating the `XML` directly on very low level base. – Axel Richter Dec 18 '18 at 07:55
  • @AxelRichter Sorry to ask you simple question , how to manipulate the XML using XWPFRun? Any example? – Arvind Dec 18 '18 at 12:04
  • @Arvind: `XWPFRun` provides `getCTR()` which returns a `org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR` which extends `org.apache.xmlbeans.XmlObject`. But as said this is to broad for a question here. So ask a new question which is more special. For example "How to create a callout shape in Word using apache poi?" Do describing exactly what you have tried already (for example how you have created a textbox shape) providing a [mcve](https://stackoverflow.com/help/mcve). And at best do providing a screenshot how this shall look like in `Word`. – Axel Richter Dec 18 '18 at 12:18
  • Thanks @AxelRichter!! your previous answer was very useful for me in other term.!!! I will do ask the new question as per your suggestion. – Arvind Dec 18 '18 at 12:26

0 Answers0