Apache POI XWPF:如何创建一组线?

Apache POI XWPF : How create a group of line?

我是 Poi 的新手。我尝试用画线制作一个组合形状。 在 LibreOffice 文档上我可以制作,但使用 Poi 似乎更难。

例子:

apache poiXWPF 中除了图表之外没有关于形状的任何内容。但是 com.microsoft.schemas.vml 能够为 Microsoft Office 文件创建 VML 图形。 Microsoft Word 也支持这些图形。

创建图形本身并不像从 VML 路径绘制那样复杂。所以只需要 VML 的知识。但是将图形插入到 Word 文档中将需要使用低级 org.openxmlformats.schemas.wordprocessingml.x2006.main.*com.microsoft.schemas.vml.* 类 并且需要有关 *.docx 的内部 XML 结构的知识文件。

希望你能从下面的完整例子中得到原理:

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

  CTShape ctShape = ctGroup.addNewShape();
  ctShape.setCoordsize("21600,21600");
  ctShape.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.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();

 }
}

此代码需要 Apache POI FAQ 中提到的所有模式的完整 jar。

下面是一个将 VML 线形放入 XWPFDocument:

的方法示例
 private static void createLineShape(XWPFParagraph paragraph, String coordsize, String vmlPath, String strokecolor, String style) throws Exception {
  XWPFRun run = paragraph.createRun(); 
  com.microsoft.schemas.vml.CTGroup ctGroup = com.microsoft.schemas.vml.CTGroup.Factory.newInstance();
  com.microsoft.schemas.vml.CTShape ctShape = ctGroup.addNewShape();
  ctShape.setCoordsize(coordsize);
  ctShape.setPath2(vmlPath);
  ctShape.setStrokecolor(strokecolor);
  ctShape.setStyle(style);
  org.w3c.dom.Node ctGroupNode = ctGroup.getDomNode(); 
  CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
  CTR cTR = run.getCTR();
  cTR.addNewPict();
  cTR.setPictArray(0, ctPicture);  
 }

可以这样调用:

...
XWPFParagraph paragraph = ...
...
createLineShape(paragraph, "21600,21600", "m 21600,0 l 0,0 l 0,21600 l 21600,21600 e", "#FF0000", "position:absolute;left:0;width:100pt;height:100pt");
...