Page 1 of 1

Drawing line

PostPosted: Tue Nov 22, 2016 1:55 am
by Olivier
Hi I have a problem with the header of a document.

I must reproduce a document but in the header there is a line. When I watch the XML, I have:
Code: Select all
               <mc:AlternateContent>
                     <mc:Choice Requires="wps">
                        <w:drawing>
                           <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251661312" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1">
                              <wp:simplePos x="0" y="0"/>
                              <wp:positionH relativeFrom="margin">
                                 <wp:posOffset>0</wp:posOffset>
                              </wp:positionH>
                              <wp:positionV relativeFrom="paragraph">
                                 <wp:posOffset>95250</wp:posOffset>
                              </wp:positionV>
                              <wp:extent cx="6985000" cy="0"/>
                              <wp:effectExtent l="0" t="0" r="0" b="0"/>
                              <wp:wrapNone/>
                              <wp:docPr id="2" name="Line 2"/>
                              <wp:cNvGraphicFramePr>
                                 <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
                              </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:cNvCnPr>
                                          <a:cxnSpLocks noChangeShapeType="1"/>
                                       </wps:cNvCnPr>
                                       <wps:spPr bwMode="auto">
                                          <a:xfrm>
                                             <a:off x="0" y="0"/>
                                             <a:ext cx="6985000" cy="0"/>
                                          </a:xfrm>
                                          <a:prstGeom prst="line">
                                             <a:avLst/>
                                          </a:prstGeom>
                                          <a:noFill/>
                                          <a:ln w="13000">
                                             <a:solidFill>
                                                <a:srgbClr val="000000"/>
                                             </a:solidFill>
                                             <a:round/>
                                             <a:headEnd/>
                                             <a:tailEnd/>
                                          </a:ln>
                                          <a:extLst>
                                             <a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">
                                                <a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">
                                                   <a:noFill/>
                                                </a14:hiddenFill>
                                             </a:ext>
                                          </a:extLst>
                                       </wps:spPr>
                                       <wps:bodyPr/>
                                    </wps:wsp>
                                 </a:graphicData>
                              </a:graphic>
                              <wp14:sizeRelH relativeFrom="page">
                                 <wp14:pctWidth>0</wp14:pctWidth>
                              </wp14:sizeRelH>
                              <wp14:sizeRelV relativeFrom="page">
                                 <wp14:pctHeight>0</wp14:pctHeight>
                              </wp14:sizeRelV>
                           </wp:anchor>
                        </w:drawing>
                     </mc:Choice>
                     <mc:Fallback>
                        <w:pict>
                           <v:line w14:anchorId="78F5CE5A" id="Line 2" o:spid="_x0000_s1026" style="position:absolute;z-index:251661312;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:margin;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page" from="0,7.5pt" to="550pt,7.5pt" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF 90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA 0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oD OlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymaiLzg5KdCXlKLjvcW893 SUOqXwnz5DrgnHtJTxOsQfEKIT7DmDSUCaxw7Rqn8787ZsmRM9e2VmPeBN4uqYvTtW7jvijg9N/y JsXecLq0q+WD6m8AAAD//wMAUEsDBBQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAX3JlbHMvLnJl b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cnMvZG93bnJldi54bWxMj0FPwzAMhe9I/IfIk7ixZEgg1DWd qklIcGMbhx3dxLQVTVKSbC38ejxxgJPt96znz+VmdoM4U0x98BpWSwWCvAm2962Gt8PT7SOIlNFb HIInDV+UYFNdX5VY2DD5HZ33uRUc4lOBGrqcx0LKZDpymJZhJM/ee4gOM4+xlTbixOFukHdKPUiH vecLHY607ch87E9Ow+to8qFuno18+Ta1PU543MVPrW8Wc70GkWnOf8twwWd0qJipCSdvkxg08COZ 1XuuF3elFHfNryKrUv7nr34AAAD//wMAUEsBAi0AFAAGAAgAAAAhALaDOJL+AAAA4QEAABMAAAAA AAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECLQAUAAYACAAAACEAOP0h/9YAAACU AQAACwAAAAAAAAAAAAAAAAAvAQAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEAa6F0pxACAAAp BAAADgAAAAAAAAAAAAAAAAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAAACEAR+OT9NkA AAAHAQAADwAAAAAAAAAAAAAAAABqBAAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAEAAQA8wAAAHAF AAAAAA== " strokeweight=".36111mm">
                              <w10:wrap anchorx="margin"/>
                           </v:line>
                        </w:pict>
                     </mc:Fallback>
                  </mc:AlternateContent>


I now mc tags exists in DOCX4J but the wps tag is unknow...
How can I do that in DOCX4j please?

Thanks for your help

Best regards

Re: Drawing line

PostPosted: Tue Nov 22, 2016 7:32 am
by jason
You can search the forum to see what mc preprocessing is doing here, but assuming you want the w:drawing:

Approach 1:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.lang.Integer;
import java.lang.Long;
import javax.xml.bind.JAXBElement;
import org.apache.xerces.dom.ElementNSImpl;
import org.docx4j.dml.CTGraphicalObjectFrameLocking;
import org.docx4j.dml.CTNonVisualDrawingProps;
import org.docx4j.dml.CTNonVisualGraphicFrameProperties;
import org.docx4j.dml.CTPoint2D;
import org.docx4j.dml.CTPositiveSize2D;
import org.docx4j.dml.Graphic;
import org.docx4j.dml.GraphicData;
import org.docx4j.dml.wordprocessingDrawing.Anchor;
import org.docx4j.dml.wordprocessingDrawing.CTEffectExtent;
import org.docx4j.dml.wordprocessingDrawing.CTPosH;
import org.docx4j.dml.wordprocessingDrawing.CTPosV;
import org.docx4j.dml.wordprocessingDrawing.CTWrapNone;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTLanguage;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;


public class Foo {
public R createRwithDrawing() {

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

R r = wmlObjectFactory.createR();

    // Create object for drawing (wrapped in JAXBElement)
    Drawing drawing = wmlObjectFactory.createDrawing();
    JAXBElement<org.docx4j.wml.Drawing> drawingWrapped = wmlObjectFactory.createRDrawing(drawing);
    r.getContent().add( drawingWrapped);
org.docx4j.dml.wordprocessingDrawing.ObjectFactory dmlwordprocessingDrawingObjectFactory = new org.docx4j.dml.wordprocessingDrawing.ObjectFactory();
        // Create object for anchor
        Anchor anchor = dmlwordprocessingDrawingObjectFactory.createAnchor();
        drawing.getAnchorOrInline().add( anchor);
org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();
            // Create object for simplePos
            CTPoint2D point2d = dmlObjectFactory.createCTPoint2D();
            anchor.setSimplePos(point2d);
                point2d.setY( 0 );
                point2d.setX( 0 );
            // Create object for positionH
            CTPosH posh = dmlwordprocessingDrawingObjectFactory.createCTPosH();
            anchor.setPositionH(posh);
                posh.setPosOffset( new Integer(0) );
                posh.setRelativeFrom(org.docx4j.dml.wordprocessingDrawing.STRelFromH.MARGIN);
            // Create object for positionV
            CTPosV posv = dmlwordprocessingDrawingObjectFactory.createCTPosV();
            anchor.setPositionV(posv);
                posv.setPosOffset( new Integer(95250) );
                posv.setRelativeFrom(org.docx4j.dml.wordprocessingDrawing.STRelFromV.PARAGRAPH);
            // Create object for extent
            CTPositiveSize2D positivesize2d = dmlObjectFactory.createCTPositiveSize2D();
            anchor.setExtent(positivesize2d);
                positivesize2d.setCx( 6985000 );
                positivesize2d.setCy( 0 );
            // Create object for effectExtent
            CTEffectExtent effectextent = dmlwordprocessingDrawingObjectFactory.createCTEffectExtent();
            anchor.setEffectExtent(effectextent);
                effectextent.setB( 0 );
                effectextent.setR( 0 );
                effectextent.setT( 0 );
                effectextent.setL( 0 );
            // Create object for wrapNone
            CTWrapNone wrapnone = dmlwordprocessingDrawingObjectFactory.createCTWrapNone();
            anchor.setWrapNone(wrapnone);
            // Create object for docPr
            CTNonVisualDrawingProps nonvisualdrawingprops = dmlObjectFactory.createCTNonVisualDrawingProps();
            anchor.setDocPr(nonvisualdrawingprops);
                nonvisualdrawingprops.setDescr( "");
                nonvisualdrawingprops.setName( "Line 2");
                nonvisualdrawingprops.setId( 2 );
            // Create object for cNvGraphicFramePr
            CTNonVisualGraphicFrameProperties nonvisualgraphicframeproperties = dmlObjectFactory.createCTNonVisualGraphicFrameProperties();
            anchor.setCNvGraphicFramePr(nonvisualgraphicframeproperties);
                // Create object for graphicFrameLocks
                CTGraphicalObjectFrameLocking graphicalobjectframelocking = dmlObjectFactory.createCTGraphicalObjectFrameLocking();
                nonvisualgraphicframeproperties.setGraphicFrameLocks(graphicalobjectframelocking);
            // Create object for graphic
            Graphic graphic = dmlObjectFactory.createGraphic();
            anchor.setGraphic(graphic);
                // Create object for graphicData
                GraphicData graphicdata = dmlObjectFactory.createGraphicData();
                graphic.setGraphicData(graphicdata);
                    graphicdata.setUri( "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
>:- No code generator for package package org.apache.xerces.dom
            anchor.setDistT( new Long(0) );
            anchor.setDistB( new Long(0) );
            anchor.setDistL( new Long(114300) );
            anchor.setDistR( new Long(114300) );
            anchor.setRelativeHeight( 251661312 );

return r;
}
}
 
Parsed in 0.025 seconds, using GeSHi 1.0.8.4


Approach 2:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
String openXML = "<w:r xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:rPr>"
                + "<w:noProof/>"
                + "<w:lang w:eastAsia=\"ja-JP\"/>"
            +"</w:rPr>"
            + "<w:drawing>"
                + "<wp:anchor allowOverlap=\"true\" behindDoc=\"false\" distB=\"0\" distL=\"114300\" distR=\"114300\" distT=\"0\" layoutInCell=\"true\" locked=\"false\" relativeHeight=\"251661312\" simplePos=\"false\">"
                    + "<wp:simplePos x=\"0\" y=\"0\"/>"
                    + "<wp:positionH relativeFrom=\"margin\">"
                        + "<wp:posOffset>0</wp:posOffset>"
                    +"</wp:positionH>"
                    + "<wp:positionV relativeFrom=\"paragraph\">"
                        + "<wp:posOffset>95250</wp:posOffset>"
                    +"</wp:positionV>"
                    + "<wp:extent cx=\"6985000\" cy=\"0\"/>"
                    + "<wp:effectExtent b=\"0\" l=\"0\" r=\"0\" t=\"0\"/>"
                    + "<wp:wrapNone/>"
                    + "<wp:docPr id=\"2\" name=\"Line 2\"/>"
                    + "<wp:cNvGraphicFramePr>"
                        + "<a:graphicFrameLocks/>"
                    +"</wp:cNvGraphicFramePr>"
                    + "<a:graphic>"
                        + "<a:graphicData uri=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">"
                            + "<wps:wsp>"
                                + "<wps:cNvCnPr>"
                                    + "<a:cxnSpLocks noChangeShapeType=\"1\"/>"

                                                  +"</wps:cNvCnPr>"
                                + "<wps:spPr bwMode=\"auto\">"
                                    + "<a:xfrm>"
                                        + "<a:off x=\"0\" y=\"0\"/>"
                                        + "<a:ext cx=\"6985000\" cy=\"0\"/>"

                                                        +"</a:xfrm>"
                                    + "<a:prstGeom prst=\"line\">"
                                        + "<a:avLst/>"

                                                        +"</a:prstGeom>"
                                    + "<a:noFill/>"
                                    + "<a:ln w=\"13000\">"
                                        + "<a:solidFill>"
                                            + "<a:srgbClr val=\"000000\"/>"

                                                              +"</a:solidFill>"
                                        + "<a:round/>"
                                        + "<a:headEnd/>"
                                        + "<a:tailEnd/>"

                                                        +"</a:ln>"
                                    + "<a:extLst>"
                                        + "<a:ext uri=\"{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}\">

                                                              +"
</a:ext>"

                                                        +"
</a:extLst>"

                                                  +"
</wps:spPr>"
                                + "
<wps:bodyPr/>"

                                            +"
</wps:wsp>"
                        +"
</a:graphicData>"
                    +"
</a:graphic>"
                +"
</wp:anchor>"
            +"
</w:drawing>"
        +"
</w:r>";
R r = (R)XmlUtils.unmarshalString(openXML);
Parsed in 0.023 seconds, using GeSHi 1.0.8.4


I've left out the elements in namespaces a14 and wp14. If you need them, you should be able to add them in easily enough.

Re: Drawing line

PostPosted: Tue Nov 22, 2016 6:32 pm
by Olivier
Thanks a lot Jason I try it