简体   繁体   中英

Apache POI XWPF : How create a group of line?

I'm new on Poi. I try make a group shape composite with drawing line. On LibreOffice doc I can make but with Poi seem be more difficult.

the exemple:

在此处输入图像描述

There is nothing about shapes in apache poi 's XWPF except charts up to now. But there is com.microsoft.schemas.vml which is able creating VML graphics for Microsoft Office files. Those graphics also are supported by Microsoft Word.

Creating the graphic itself is not as complicated as it is drawn from an VML path. So only knowledge about VML is needed. But inserting the graphic into the Word document will need usage of the low level org.openxmlformats.schemas.wordprocessingml.x2006.main.* and com.microsoft.schemas.vml.* classes and needs knowledge about the internal XML structure of a *.docx file.

I hope you can get the principle from the following complete example:

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 com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import com.microsoft.schemas.vml.CTShapetype;

import org.w3c.dom.Node;

public class CreateWordPathShape {

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

  String boxWidth = "100pt";
  String boxHeight = "100pt";
  String posLeft = "150pt";
  String posTop = "0pt";

  XWPFDocument doc= new XWPFDocument();

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

  CTGroup ctGroup = CTGroup.Factory.newInstance();

  String shapeTypeId = "_x0000_t123";
  CTShapetype shapetype = ctGroup.addNewShapetype();
  shapetype.setId(shapeTypeId);
  shapetype.setSpt(123);
  
  shapetype.setCoordsize("21600,21600");
  shapetype.setPath2("m 21600,0 l 0,0 l 0,21600 l 21600,21600 e");
  //path: from 0,0 (top left) move to 21600,0 (top right), line to 0,0 (top left), line to 0,21600 (bottom left), line to 21600,21600 (bottom right), end

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.setType("#"+shapeTypeId);
  ctShape.setStyle("position:absolute"
   +";top:" + posTop
   +";left:" + posLeft
   +";width:" + boxWidth
   +";height:" + boxHeight
  );

  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();

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

 }
}

This code needs the full jar of all of the schemas as mentioned in Apache POI FAQ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM