Show a text in a box in Word by POI

128 views Asked by At

I want to show a text in Word inside a box using Apache POI XWPF like this: text in a box

This example was done in Word using a shape with a text inside the shape.

But I am not able to do this correctly in Word using XWPF. When I try:

   public void createDocument() throws IOException {
      XWPFDocument doc = new XWPFDocument();
      XWPFParagraph paragraph = doc.createParagraph();
      XWPFRun run = paragraph.createRun();
      run.setText("The text");

      CTGroup ctGroup = CTGroup.Factory.newInstance();
      CTShape ctShape = ctGroup.addNewShape();
      ctShape.setStyle("width:100pt;height:50pt");
      ctShape.setFillcolor("FF0000");

      Node ctGroupNode = ctGroup.getDomNode();
      try {
         CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
         CTR cTR = run.getCTR();
         cTR.addNewPict();
         cTR.setPictArray(0, ctPicture);
      } catch (XmlException ex) {
      }

      try ( OutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) {
         doc.write(stream);
      }
      doc.close();
   }

I have a Shape, but the text is not inside the Shape, and the Shape is not filled.

I used this example: create text box in document .docx using apache poi

1

There are 1 answers

1
Axel Richter On BEST ANSWER

My examples in create text box in document .docx using apache poi uses Microsoft's VML Object Model.

The last example also works using current Apache POI 5.2.4.

The text in the text-box must be within a text box content. This code line set it:

...
ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");
...

But if fill a color shall be used, the surrounding shape would must have a closed line around. Either one would set a Path to describe the shape outlines. Or - easier - one would use a rectangle shape instead of a simple shape. Rectangle shape in Apache POI for Word is a com.microsoft.schemas.vml.CTRect instead com.microsoft.schemas.vml.CTShape.

Following works for me:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;

import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTRect;
import com.microsoft.schemas.office.word.CTWrap;
import com.microsoft.schemas.office.word.STWrapType;

import org.w3c.dom.Node;

public class CreateWordTextBox {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body text: ");

  CTGroup ctGroup = CTGroup.Factory.newInstance();

  CTRect ctRect = ctGroup.addNewRect();
  ctRect.addNewWrap().setType(STWrapType.SQUARE);
  ctRect.setStyle("position:absolute;mso-position-horizontal:center;margin-top:40pt;width:100pt;height:24pt");
  ctRect.setFillcolor("#FFFF00");
  CTTxbxContent ctTxbxContent = ctRect.addNewTextbox().addNewTxbxContent();
  ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");

  Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  run=paragraph.createRun();  
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... ");

  FileOutputStream out = new FileOutputStream("WordTextBox.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}

Produces:

enter image description here