Page 1 of 1

Add an ellipse to docx

PostPosted: Wed Nov 19, 2014 1:17 am
by pacoabato
Hello all,

I'm trying to add an ellipse to a new docx document:

Code: Select all
String xmlEllipse = getXmlEllipse();
Object ellipse = XmlUtils.unmarshalString(xmlEllipse);
Graphic ellipseGraphic = (Graphic) ((JAXBElement)ellipse).getValue();

Anchor anchor = new Anchor();
anchor.setGraphic(ellipseGraphic);

ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(anchor);

WordprocessingMLPackage newDoc = WordprocessingMLPackage.createPackage();
newDoc.getMainDocumentPart().getContent().add(paragraph);

newDoc.save(new File(dirPath + outputFilePath));


The xml that I'm using for the ellipse is this one:

Code: Select all
private static String getXmlEllipse() {
        String xmlEllipse = ""
                + " <a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">"
                + "     <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>";

        return xmlEllipse;
    }


But I'm getting a docx file that show a blank page when I open it with LibreOffice (MS Word fails to open it).
The document.xml inside the generated docx file contains the graphic ellipse tags, it is this:

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns8="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 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:w10="urn:schemas-microsoft-com:office:word" xmlns:v="urn:schemas-microsoft-com:vml" 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="01b73d7" w14:textId="01b73d7">
            <w:pPr>
                <w15:collapsed w:val="false"/>
            </w:pPr>
            <w:r>
                <w:drawing>
                    <wp:anchor relativeHeight="0" behindDoc="false" locked="false" layoutInCell="false" allowOverlap="false">
                        <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">
                                    <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>


What am I doing wrong or missing?

Thanks for any advice.

Re: Add an ellipse to docx

PostPosted: Wed Nov 19, 2014 11:00 am
by jason
Answered at your crosspost at http://stackoverflow.com/questions/2699 ... 0#27006500

Please don't do that!

Re: Add an ellipse to docx

PostPosted: Wed Nov 19, 2014 8:06 pm
by pacoabato
Sorry for crossposting. I wrote there before finding out this forum and I didn't know that you are aware of SO.

I'll continue the conversation there. Thank you very much.