Skip to content

Drawing

DrawingML介绍

diagram — 使用一组相关的颜色、数据、布局和样式部件显示的图片或图形表示形式。 图表类型的示例有循环图、组织图、金字塔图、目标图和维恩图

包中每个图表(diagram)应包含一个图表数据部件(Diagram Data part)。 每个图表数据部件应是

WordprocessingML:

图片类型 格式
image/gif http://www.w3.org/Graphics/GIF/spec-gif89a.txt
image/png ISO/IEC 15948:2003 http://www.libpng.org/pub/png/spec/
image/tiff http://partners.adobe.com/public/developer/tiff/index.html#spec
image/pict http://developer.apple.com/documentation/mac/QuickDraw/QuickDraw2.html
image/jpeg http://www.w3.org/Graphics/JPEG/

图像可以作为 ZIP 项存储在包中。 图像 ZIP 项应通过图像部件关系和适当的内容类型来标识

图像部件可以位于包含关系部件的包的内部或外部(从语法上表达,关系元素的 TargetMode 属性可以是 Internal 或 External)。

图像部件不应与 ECMA-376 定义的其他部件有隐式或显式关系。

想要互操作性的生产者应该使用以下标准格式之一:

GUID 用途
5B0F96BF-4C86-4D44-8424-2F8E86C0A820 图形效果兼容性(如透明度)
96DAC541-7B7A-4539-8514-4CE9B76B6CEE SVG 矢量图形支持
28A0092B-C50C-407E-A947-70E740481C1C 图表动画效果

XML常用部分结构

w:r
|
|-- w:drawing
|   |-- wp:inline 
|       |-- wp:extent
|       |-- wp:effectExtent
|       |-- wp:docPr
|       |-- wp:cNvGraphicFramePr
|       |-- a:graphic
c#
/word/document.xml
| -/word/media/image.type

 public void CreateImagePart(ImagePart part)
        {
            GeneratePartContent(part);

        }

        // Generates content of part.
        private void GeneratePartContent(ImagePart part)
        {
            System.IO.Stream data = GetBinaryDataStream(partData);
            part.FeedData(data);
            data.Close();
        }

        #region Binary Data
        private string partData = "iVBORw0KGgoAAAANSUhEUgAAADUAAAA5CAYAAACWJGMLAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABPnSURBVGhD7Zp3fFRl1scREjrSe1UU0cXOvq66RV1ZQVFCJyG90EITgg..................................";

        private System.IO.Stream GetBinaryDataStream(string base64String)
        {
            return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
        }

        #endregion

XML层级结构例子

xml
<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="69450121" wp14:editId="726B8B81" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
    <wp:extent cx="504895" cy="543001" />
    <wp:effectExtent l="0" t="0" r="0" b="9525" />
    <wp:docPr id="1439967217" name="图片 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.openxmlformats.org/drawingml/2006/picture">
        <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
          <pic:nvPicPr>
            <pic:cNvPr id="1439967217" name="图片 1" />
            <pic:cNvPicPr />
          </pic:nvPicPr>
          <pic:blipFill>
            <a:blip r:embed="rId7" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
              <a:extLst>
                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                  <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
                </a:ext>
              </a:extLst>
            </a:blip>
            <a:stretch>
              <a:fillRect />
            </a:stretch>
          </pic:blipFill>
          <pic:spPr>
            <a:xfrm>
              <a:off x="0" y="0" />
              <a:ext cx="504895" cy="543001" />
            </a:xfrm>
            <a:prstGeom prst="rect">
              <a:avLst />
            </a:prstGeom>
          </pic:spPr>
        </pic:pic>
      </a:graphicData>
    </a:graphic>
  </wp:inline>
</w:drawing>

wp:inline

wp:inline 是一个内嵌的图形对象,它包含图形的尺寸、位置、效果、文档属性和图形数据等信息。

wp:extent

wp:extent 是一个表示图形对象尺寸的元素,它包含图形的宽度和高度,单位是EMU(English Metric Unit,英制单位)。

属性 类型 说明
cx string 图形的宽度,以EMU为单位
cy string 图形的高度,以EMU为单位

wp:effectExtent

定义图形特效(如阴影、边框)的 ​延伸范围

属性 类型 说明
l string 图形的宽度,以EMU为单位
t string 图形的高度,以EMU为单位
r string 图形的宽度,以EMU为单位
b string 图形的高度,以EMU为单位

wp:docPr

wp:docPr 是一个表示图形对象的文档属性的元素,它包含图形的ID、名称、位置和大小等信息。

属性 类型 说明
id string 图形的ID
name string 图形的名称
desc string 图形的描述

wp:cNvGraphicFramePr

定义图形的 ​非可视化属性​(如锁定纵横比)

a:graphic

a:graphicData

pic:pic
pic:nvPicPr
pic:cNvPr
属性 类型 说明
id string 图形的ID
name string 图形的名称
pic:cNvPicPr
pic:blipFill
a:blip
属性 类型 说明
r:embed string 图像的ID,对应word/media/image.type
a:extLst
a:ext

拓展类型,用于处理兼容性的

属性 类型 说明
uri string 拓展类型,GUID
a:stretch

填充相关

a:fillRect
属性 类型 说明
l string 图形的宽度,以EMU为单位
t string 图形的高度,以EMU为单位
r string 图形的宽度,以EMU为单位
b string 图形的高度,以EMU为单位
a:fillRect
pic:spPr
a:xfrm
a:off
属性 类型 说明
x string 水平偏移,以EMU为单位
y string 垂直偏移,以EMU为单位
a:ext
属性 类型 说明
cx string 图形的宽度,以EMU为单位
cy string 图形的高度,以EMU为单位
a:prstGeom
a:avLst

IMPORTANT

1 cm = 360,000 EMU

创建一个内嵌的图形对象

c#
 string imageId = AddImageToMainPart(mainPart, imagePath);


 string imageName = Path.GetFileName(imagePath);

 var cx = Convert_CM_To_Emu(widthCm);
 var cy = Convert_CM_To_Emu(heightCm);

 var run = new Run();
 var drawing = new Drawing(
     new DW.Inline(
         new DW.Extent { Cx = cx, Cy = cy }, 
         new DW.DocProperties { Id = 1, Name = "GeneratedImage" },
         new DW.NonVisualGraphicFrameDrawingProperties(
             new A.GraphicFrameLocks { NoChangeAspect = true }
 ),
 new A.Graphic(
             new A.GraphicData(
 new PIC.Picture(
                     new PIC.NonVisualPictureProperties(
                         new PIC.NonVisualDrawingProperties { Id = GenerateUniqueId(), Name = imageName },
                         new PIC.NonVisualPictureDrawingProperties()
                     ),
                     new PIC.BlipFill(
                         new A.Blip(
                             new A.BlipExtensionList(
                                 new A.BlipExtension { Uri = "{5B0F96BF-4C86-4D44-8424-2F8E86C0A820}" }
                             )
                         )
                         {
                             Embed = imageId,
                             CompressionState = A.BlipCompressionValues.Print
                         },
                         new A.Stretch(new A.FillRectangle())
                     ),
                     new PIC.ShapeProperties(
                         new A.Transform2D(
                             new A.Offset { X = 0L, Y = 0L },
                             new A.Extents { Cx = cx, Cy = cy }
                         ),
                         new A.PresetGeometry(
                             new A.AdjustValueList()
                         )
                         { Preset = A.ShapeTypeValues.Rectangle }
                     )
                 )
             )
             { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
         )
     )
     { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 }
 );

 run.Append(drawing);

更多内容