2

I am dynamically creating Word documents using the Velocity template engine. The documents are created in XML (using the Microsoft Office 2003 WordprocessML XML standard).

I am embedding an image (encoded in Base64 format) inside the WordML in the traditional way:

<w:pict>  
    <v:shapeType> ... </v:shapeType>    
    <w:binData w:name="wordml://02000002.jpg"> [ encoded data] </w:binData>

    <v:shape id="_x0000_s1026"  
           type="#_x0000_t75"
           style="width:100%;height:100%" 
           o:allowoverlap="f"
           >
        <v:imagedata src="wordml://02000002.jpg" o:title="testimage2">
        </v:imagedata>
    </v:shape>

</w:pict>

The issue is with image sizing / scaling. Note the style attribute on the <v:shape> tag. Setting height and width to 100% does not size the image to its actual size. It sets height and width to approximately 1.04 inches which skews the image. Leaving off the height and width has the same effect. Setting height and width to auto has the same effect.

Most annoyingly, if I open the generated document in Word 2003, right click on the image, open the Format Picture dialog, and go to the Size tab it display the correct height and width under "Original size." It even provides a "Reset" button that resizes the image to its original size (assuming the "Relative to original picture size" checkbox is checked.)

I am like: !*@&#^#% ???

So my question is:

Is there anyway in the context of WordML to get the original size values for the image (or instruct Word to use the original image size)?

If someone can help will this question, I promise I will write an in depth blog post to ensure that I will be the last person that has to deal with this.

4

3 回答 3

1

如果您使用的是 xslt 模板,您可以像这样在模板中编写一些 C# 脚本(它用于元文件(.wmf),但您可以将其重写为 Bitmap 类以与 .jpg 格式一起使用):

<msxsl:script language="C#" implements-prefix="user">
     <msxsl:assembly name="System.Drawing" />
    <msxsl:using namespace="System.Drawing.Imaging" />


     <![CDATA[

          public static double getImageWidthFrom64String(string base64)
            {
            byte[] bytes = Convert.FromBase64String(file);
            System.Drawing.Imaging.Metafile image;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
            {
                image =
(System.Drawing.Imaging.Metafile)System.Drawing.Imaging.Metafile.FromStream(ms);
            }
       return image.Width;
            }

             public static double getImageHeightFrom64String(string base64)
            {
            byte[] bytes = Convert.FromBase64String(file);
            System.Drawing.Imaging.Metafile image;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
            {
                image =
(System.Drawing.Imaging.Metafile)System.Drawing.Imaging.Metafile.FromStream(ms);
            }      
            return image.Height;
            }
     ]]>
      </msxsl:script>

然后你可以使用这些函数在你的模板中获得真实的图像大小:

<xsl:template match="/ns1:Work/ns1:leaderSignature">
    <ns1:picture>
      <xsl:for-each select="@ns1:*|@*[namespace-uri()='']">
        <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:variable name="filename" select="concat(concat('leadsig',ancestor::ns1:Work/ns1:id),'.wmz')"/>
      <xsl:variable name="file64String" select="."></xsl:variable> 
      <w:r>
        <w:rPr>
          <w:noProof />
        </w:rPr>
        <w:pict>    
          <w:binData w:name="{concat('wordml:\\',$filename)}">
            <xsl:value-of select="." />
          </w:binData>
          <v:shape id="pic3" o:spid="_x0000_s1028"  style="
            width:{user:getImageWidthFrom64String($file64String)};
            height:{user:getImageHeightFrom64String($file64String)};
            position:absolute;         
            text-align:left;
            left:0;
            margin-left:430pt;
            margin-top:192pt;       
            z-index:2;   
            visibility:visible;
            mso-position-horizontal-relative:page;
            mso-position-horizontal:absolute;
            mso-position-vertical-relative:page;
            mso-position-vertical:absolute;   
            mso-wrap-style:square;
            mso-wrap-distance-left:9pt;
            mso-wrap-distance-top:0;
            mso-wrap-distance-right:9pt;
            mso-wrap-distance-bottom:0">         
            <v:imagedata src="{concat('wordml:\\',$filename)}" o:title="" />
           <w10:wrap anchorx="page" anchory="page" />
          </v:shape>
        </w:pict>  
      </w:r>
    </ns1:picture>
  </xsl:template><!--Leader Signature-->
于 2013-11-14T06:29:23.240 回答
0

我不明白您为什么要依靠 WordML 来获取图像大小?您正在将图像输入 Word 文档,您可以使用 Bitmap 类获取图像大小。

于 2012-03-14T16:26:26.220 回答
0

您需要将“v:shape”元素的“style”属性中的宽度和高度设置为 0px。例如:

<v:shape style="width:0px;height:0px" >

并且图像将具有原始大小。

于 2010-08-03T14:59:35.033 回答