我是新来的。我试着用画线制作成一组形状的复合材料。在LibreOffice文档上,我可以制作,但是使用Poi似乎更困难。
例如:
发布于 2022-01-06 13:35:32
到目前为止,除了图表之外,apache poi
的XWPF
中没有任何关于图形的内容,但是com.microsoft.schemas.vml
可以为Microsoft文件创建VML图形,这些图形也被Microsoft所支持。
创建图形本身并不像从VML路径中提取图形那样复杂。因此,只需要有关VML的知识。但是将图形插入Word文档需要使用低级的org.openxmlformats.schemas.wordprocessingml.x2006.main.*
和com.microsoft.schemas.vml.*
类,并且需要了解*.docx
文件的内部*.docx
结构。
我希望你能从以下完整的例子中得到这个原则:
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常见问题中提到的所有模式的完整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");
...
https://stackoverflow.com/questions/70604410
复制相似问题