Page 1 of 1

v:shape drawing lines and arrows

PostPosted: Wed Sep 24, 2014 8:24 pm
by AndreasN
Hi,

Is it possible to draw lines programmatically with the framework? I noticed that Word produce a <v:shape /> with o:gfxdata.

Kind regards,
Andreas

Re: v:shape drawing lines and arrows

PostPosted: Wed Sep 24, 2014 10:42 pm
by jason
In package org.docx4j.vml there is

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
public class CTShape implements Child, VmlShapeElements, VmlAllCoreAttributes, VmlAllShapeAttributes
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


https://github.com/plutext/docx4j/blob/ ... Shape.java

but we don't have a higher level API than this.

If you'd like to discuss further, please include sample vml xml. For it to display nicely here, use square bracket tags containing element name xml, thanks.

Re: v:shape drawing lines and arrows

PostPosted: Thu Sep 25, 2014 4:17 am
by AndreasN
I want draw some lines relate to defined positions within the document. What I'm wondering is the different output between "Word for mac 2011" and Word 2010.

Below is the generated code with the Mac version and it looks like what I want without binary encoded data, as the outcome of Windows Word.

Code: Select all
<w:r>
<w:rPr>
  <w:noProof/>
    </w:rPr>
    <w:pict>
     <v:line from="162pt,36pt" to="261pt,36pt" 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:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin" id="Gerade Verbindung 2" o:spid="_x0000_s1026" strokecolor="black [3213]" strokeweight="1pt">
        <v:stroke startarrow="oval" startarrowwidth="narrow" startarrowlength="short" endarrow="oval" endarrowwidth="wide"/>
        <v:shadow on="t" opacity="24903f" offset="0,20000emu" origin=",.5"/>
     </v:line>
     </w:pict>
</w:r>


The next challenge is the measurement of Tc objects for instance to calculate the coordinates of the <v:line /> to "draw" on a particular area.

Is it possible to get the coordinates of Tc objects or whatever element I want?

Re: v:shape drawing lines and arrows

PostPosted: Thu Sep 25, 2014 9:23 am
by jason
AndreasN wrote:The next challenge is the measurement of Tc objects for instance to calculate the coordinates of the <v:line /> to "draw" on a particular area.

Is it possible to get the coordinates of Tc objects or whatever element I want?


It is not currently possible to get the coordinates of an object, because that would require us to have a page layout model identical to Word's (which we don't have).

The good news though is that you shouldn't need it, if you use a canvas, and position that canvas "relative" to its context, rather than absolute. If you use a canvas, the numbers inside it are all relative to that, not the page.

Before you jump in to creating VML, please be aware that DrawingML is probably a better option. When you add stuff to a canvas is Word 2010, then save the docx, you end up with something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<mc:AlternateContent>
  <mc:Choice Requires="wpc">
    <w:drawing>
      <wp:inline distT="0" distB="0" distL="0" distR="0">
        <wp:extent cx="2286000" cy="1333500"/>
        <wp:effectExtent l="0" t="0" r="0" b="0"/>
        <wp:docPr id="1" name="Canvas 1"/>
        <wp:cNvGraphicFramePr>
          <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
        </wp:cNvGraphicFramePr>
        <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
          <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas">
            <wpc:wpc>
              <wpc:bg/>
              <wpc:whole/>
              <wps:wsp>
                <wps:cNvPr id="2" name="Straight Connector 2"/>
                <wps:cNvCnPr/>
                <wps:spPr>
                  <a:xfrm>
                    <a:off x="85725" y="142875"/>
                    <a:ext cx="1905000" cy="971550"/>
                  </a:xfrm>
                  <a:prstGeom prst="line">
                    <a:avLst/>
                  </a:prstGeom>
                </wps:spPr>
                <wps:style>
                  <a:lnRef idx="1">
                    <a:schemeClr val="accent1"/>
                  </a:lnRef>
                  <a:fillRef idx="0">
                    <a:schemeClr val="accent1"/>
                  </a:fillRef>
                  <a:effectRef idx="0">
                    <a:schemeClr val="accent1"/>
                  </a:effectRef>
                  <a:fontRef idx="minor">
                    <a:schemeClr val="tx1"/>
                  </a:fontRef>
                </wps:style>
                <wps:bodyPr/>
              </wps:wsp>
            </wpc:wpc>
          </a:graphicData>
        </a:graphic>
      </wp:inline>
    </w:drawing>
  </mc:Choice>
  <mc:Fallback>
    <w:pict>
      <v:group id="Canvas 1" o:spid="_x0000_s1026" editas="canvas" style="width:180pt;height:105pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line" coordsize="22860,13335" o:gfxdata="UEsDBBQABgAIAA...AAAABAAEAPMAAABKBQAAAAA=&#xA;">
        <v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
          <v:stroke joinstyle="miter"/>
          <v:formulas>
            <v:f eqn="if lineDrawn pixelLineWidth 0"/>
            <v:f eqn="sum @0 1 0"/>
            <v:f eqn="sum 0 0 @1"/>
            <v:f eqn="prod @2 1 2"/>
            <v:f eqn="prod @3 21600 pixelWidth"/>
            <v:f eqn="prod @3 21600 pixelHeight"/>
            <v:f eqn="sum @0 0 1"/>
            <v:f eqn="prod @6 1 2"/>
            <v:f eqn="prod @7 21600 pixelWidth"/>
            <v:f eqn="sum @8 21600 0"/>
            <v:f eqn="prod @7 21600 pixelHeight"/>
            <v:f eqn="sum @10 21600 0"/>
          </v:formulas>
          <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
          <o:lock v:ext="edit" aspectratio="t"/>
        </v:shapetype>
        <v:shape id="_x0000_s1027" type="#_x0000_t75" style="position:absolute;width:22860;height:13335;visibility:visible;mso-wrap-style:square">
          <v:fill o:detectmouseclick="t"/>
          <v:path o:connecttype="none"/>
        </v:shape>
        <v:line id="Straight Connector 2" o:spid="_x0000_s1028" style="position:absolute;visibility:visible;mso-wrap-style:square" from="857,1428" to="19907,11144" o:connectortype="straight" o:gfxdata="UEsDB...RAwAAAAA=&#xA;" strokecolor="#4579b8 [3044]"/>
        <w10:anchorlock/>
      </v:group>
    </w:pict>
  </mc:Fallback>
</mc:AlternateContent>
 
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


That is, the content expressed in both DrawingML and VML. In this case, the DrawingML will be used if the consuming application supports "wpc".

On load, docx4j selects one of the options (and discards the other).

But there is nothing to stop you choosing to use and add DrawingML (ie without the enclosing mc elements, and instead of legacy VML and its @o:gfxdata).

Re: v:shape drawing lines and arrows

PostPosted: Fri Sep 26, 2014 7:08 am
by AndreasN
It tried the canvas approach and fiddled around in word to get it positioned relative to an table and tried resizing it by its properties to cover the table... The relation seems only set to the table cell and there seems no chance to get it right. I give up for the moment, scheduled...