我设法向 docx 文档添加一个椭圆,但未能添加多个椭圆。
这是我使用的代码:
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
String xmlAnchor = getXmlAnchor(607000, 288000, 1);
Object anchorXml = XmlUtils.unmarshalString(xmlAnchor);
Anchor anchor = (Anchor) ((JAXBElement)anchorXml).getValue();
String xmlAnchor2 = getXmlAnchor(1110000, 1000000, 2);
Object anchorXml2 = XmlUtils.unmarshalString(xmlAnchor2);
Anchor anchor2 = (Anchor) ((JAXBElement)anchorXml2).getValue();
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
P paragraph2 = factory.createP();
R run = factory.createR();
R run2 = factory.createR();
Drawing drawing = factory.createDrawing();
Drawing drawing2 = factory.createDrawing();
paragraph.getContent().add(run);
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(anchor);
paragraph2.getContent().add(run2);
run2.getContent().add(drawing2);
drawing2.getAnchorOrInline().add(anchor2);
wordMLPackage.getMainDocumentPart().getContent().add(0, paragraph);
wordMLPackage.getMainDocumentPart().getContent().add(0, paragraph2);
wordMLPackage.save(new File(outputFilePath));
这是 getXmlAnchor 方法:
private static String getXmlAnchor(int x, int y, int id) {
String xmlAnchor = ""
+ "<wp:anchor xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" distT=\"0\" distB=\"0\" distL=\"114300\" distR=\"114300\" simplePos=\"0\" relativeHeight=\"251659264\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
+ " <wp:simplePos x=\"0\" y=\"0\"/>"
+ " <wp:positionH relativeFrom=\"page\">"
+ " <wp:posOffset>" + x + "</wp:posOffset>"
+ " </wp:positionH>"
+ " <wp:positionV relativeFrom=\"page\">"
+ " <wp:posOffset>" + y + "</wp:posOffset>"
+ " </wp:positionV>"
+ " <wp:extent cx=\"130629\" cy=\"130629\"/>"
+ " <wp:effectExtent l=\"0\" t=\"0\" r=\"22225\" b=\"22225\"/>"
+ " <wp:wrapNone/>"
+ " <wp:docPr id=\"1\" name=\"" + id +" Elipse\"/>"
+ " <wp:cNvGraphicFramePr/>"
+ " <a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
+ " <a:graphicData uri=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">"
+ " <wps:wsp>"
+ " <wps:cNvSpPr/>"
+ " <wps:spPr>"
+ " <a:xfrm>"
+ " <a:off x=\"0\" y=\"0\"/>"
+ " <a:ext cx=\"130629\" cy=\"130629\"/>"
+ " </a:xfrm>"
+ " <a:prstGeom prst=\"ellipse\">"
+ " <a:avLst/>"
+ " </a:prstGeom>"
+ " </wps:spPr>"
+ " <wps:style>"
+ " <a:lnRef idx=\"2\">"
+ " <a:schemeClr val=\"accent1\">"
+ " <a:shade val=\"50000\"/>"
+ " </a:schemeClr>"
+ " </a:lnRef>"
+ " <a:fillRef idx=\"1\">"
+ " <a:schemeClr val=\"accent1\"/>"
+ " </a:fillRef>"
+ " <a:effectRef idx=\"0\">"
+ " <a:schemeClr val=\"accent1\"/>"
+ " </a:effectRef>"
+ " <a:fontRef idx=\"minor\">"
+ " <a:schemeClr val=\"lt1\"/>"
+ " </a:fontRef>"
+ " </wps:style>"
+ " <wps:bodyPr rot=\"0\" spcFirstLastPara=\"0\" vertOverflow=\"overflow\" horzOverflow=\"overflow\" vert=\"horz\" wrap=\"square\" lIns=\"91440\" tIns=\"45720\" rIns=\"91440\" bIns=\"45720\" numCol=\"1\" spcCol=\"0\" rtlCol=\"0\" fromWordArt=\"0\" anchor=\"ctr\" anchorCtr=\"0\" forceAA=\"0\" compatLnSpc=\"1\">"
+ " <a:prstTxWarp prst=\"textNoShape\">"
+ " <a:avLst/>"
+ " </a:prstTxWarp>"
+ " <a:noAutofit/>"
+ " </wps:bodyPr>"
+ " </wps:wsp>"
+ " </a:graphicData>"
+ " </a:graphic>"
+ " <wp14:sizeRelH relativeFrom=\"margin\">"
+ " <wp14:pctWidth>0</wp14:pctWidth>"
+ " </wp14:sizeRelH>"
+ " <wp14:sizeRelV relativeFrom=\"margin\">"
+ " <wp14:pctHeight>0</wp14:pctHeight>"
+ " </wp14:sizeRelV>"
+ "</wp:anchor>";
return xmlAnchor;
}
但是docx文件无法用MS Office打开。我还尝试将两次运行添加到同一段落中,等等。似乎都不起作用。
这是生成的 docx 文件中的 document.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" mc:Ignorable="w14 w15">
<w:body>
<w:p w14:paraId="6ab5171" w14:textId="6ab5171">
<w:pPr>
<w15:collapsed w:val="false"/>
</w:pPr>
<w:r>
<w:drawing>
<wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="false" relativeHeight="251659264" behindDoc="false" locked="false" layoutInCell="true" allowOverlap="true">
<wp:simplePos x="0" y="0"/>
<wp:positionH relativeFrom="page">
<wp:posOffset>1110000</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="page">
<wp:posOffset>1000000</wp:posOffset>
</wp:positionV>
<wp:extent cx="130629" cy="130629"/>
<wp:effectExtent l="0" t="0" r="22225" b="22225"/>
<wp:wrapNone/>
<wp:docPr id="1" name="2 Elipse"/>
<wp:cNvGraphicFramePr/>
<a:graphic>
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing">
<wps:cNvSpPr/>
<wps:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="130629" cy="130629"/>
</a:xfrm>
<a:prstGeom prst="ellipse">
<a:avLst/>
</a:prstGeom>
</wps:spPr>
<wps:style>
<a:lnRef idx="2">
<a:schemeClr val="accent1">
<a:shade val="50000"/>
</a:schemeClr>
</a:lnRef>
<a:fillRef idx="1">
<a:schemeClr val="accent1"/>
</a:fillRef>
<a:effectRef idx="0">
<a:schemeClr val="accent1"/>
</a:effectRef>
<a:fontRef idx="minor">
<a:schemeClr val="lt1"/>
</a:fontRef>
</wps:style>
<wps:bodyPr anchor="ctr" anchorCtr="0" bIns="45720" compatLnSpc="1" forceAA="0" fromWordArt="0" horzOverflow="overflow" lIns="91440" numCol="1" rIns="91440" rot="0" rtlCol="0" spcCol="0" spcFirstLastPara="0" tIns="45720" vert="horz" vertOverflow="overflow" wrap="square">
<a:prstTxWarp prst="textNoShape">
<a:avLst/>
</a:prstTxWarp>
<a:noAutofit/>
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>
</w:r>
</w:p>
<w:p>
<w:r>
<w:drawing>
<wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="false" relativeHeight="251659264" behindDoc="false" locked="false" layoutInCell="true" allowOverlap="true">
<wp:simplePos x="0" y="0"/>
<wp:positionH relativeFrom="page">
<wp:posOffset>607000</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="page">
<wp:posOffset>288000</wp:posOffset>
</wp:positionV>
<wp:extent cx="130629" cy="130629"/>
<wp:effectExtent l="0" t="0" r="22225" b="22225"/>
<wp:wrapNone/>
<wp:docPr id="1" name="1 Elipse"/>
<wp:cNvGraphicFramePr/>
<a:graphic>
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing">
<wps:cNvSpPr/>
<wps:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="130629" cy="130629"/>
</a:xfrm>
<a:prstGeom prst="ellipse">
<a:avLst/>
</a:prstGeom>
</wps:spPr>
<wps:style>
<a:lnRef idx="2">
<a:schemeClr val="accent1">
<a:shade val="50000"/>
</a:schemeClr>
</a:lnRef>
<a:fillRef idx="1">
<a:schemeClr val="accent1"/>
</a:fillRef>
<a:effectRef idx="0">
<a:schemeClr val="accent1"/>
</a:effectRef>
<a:fontRef idx="minor">
<a:schemeClr val="lt1"/>
</a:fontRef>
</wps:style>
<wps:bodyPr anchor="ctr" anchorCtr="0" bIns="45720" compatLnSpc="1" forceAA="0" fromWordArt="0" horzOverflow="overflow" lIns="91440" numCol="1" rIns="91440" rot="0" rtlCol="0" spcCol="0" spcFirstLastPara="0" tIns="45720" vert="horz" vertOverflow="overflow" wrap="square">
<a:prstTxWarp prst="textNoShape">
<a:avLst/>
</a:prstTxWarp>
<a:noAutofit/>
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>
</w:r>
</w:p>
<w:sectPr>
<w:pgSz w:w="11907" w:h="16839" w:code="9"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>
</w:sectPr>
</w:body>
</w:document>
ImageAdd示例(https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/ImageAdd.java)添加了三个段落,没有明显的复杂性。 我尝试了 wordMLPackage.getMainDocumentPart().addObject(paragraph) 但都没有成功。
完成多个元素插入的正确方法是什么?
在此更改ID