Search code examples
c#asp.net.netms-wordopenxml

How to add a shape in word document using openxml C#?


I want to add a horizontal shape in the word document using openxml.

I tried to paste the code using OpenXML Productivity tool but it is giving some error while opening the generated file (The "http://schemas.microsoft.com/office/word/2010/wordml:anchorId" attribute is not declared)

Please see the below image that same shape I want to add to my document. enter image description here


Solution

  • Posting my solution which I got after my 1 hour of findings in OpenXML.

      //namespace used
      using DocumentFormat.OpenXml;
      using W = DocumentFormat.OpenXml.Wordprocessing;
      using Wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
      using A = DocumentFormat.OpenXml.Drawing;
      using Wps = DocumentFormat.OpenXml.Office2010.Word.DrawingShape;
      using V = DocumentFormat.OpenXml.Vml;
      using Wvml = DocumentFormat.OpenXml.Vml.Wordprocessing;
      using A14 = DocumentFormat.OpenXml.Office2010.Drawing;
      using Wp14 = DocumentFormat.OpenXml.Office2010.Word.Drawing;
      using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
      using Ovml = DocumentFormat.OpenXml.Vml.Office
      //namespace used    
    
        public Run AddShape()
        {
            Run run1 = new Run();
    
            RunProperties runProperties1 = new RunProperties();
            NoProof noProof1 = new NoProof();
    
            runProperties1.Append(noProof1);
    
            AlternateContent alternateContent1 = new AlternateContent();
    
            AlternateContentChoice alternateContentChoice1 = new AlternateContentChoice() { Requires = "wps" };
    
            Drawing drawing1 = new Drawing();
    
            Wp.Anchor anchor1 = new Wp.Anchor() { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, SimplePos = false, RelativeHeight = (UInt32Value)251658240U, BehindDoc = true, Locked = false, LayoutInCell = false, AllowOverlap = true };
            anchor1.AnchorId = GetRandomHexNumber(8);
            Wp.SimplePosition simplePosition1 = new Wp.SimplePosition() { X = 0L, Y = 0L };
    
            Wp.HorizontalPosition horizontalPosition1 = new Wp.HorizontalPosition() { RelativeFrom = Wp.HorizontalRelativePositionValues.Page };
            Wp.PositionOffset positionOffset1 = new Wp.PositionOffset();
            positionOffset1.Text = "0";
    
            horizontalPosition1.Append(positionOffset1);
    
            Wp.VerticalPosition verticalPosition1 = new Wp.VerticalPosition() { RelativeFrom = Wp.VerticalRelativePositionValues.Page };
            Wp.PositionOffset positionOffset2 = new Wp.PositionOffset();
            positionOffset2.Text = ShapeVPosition;
    
            verticalPosition1.Append(positionOffset2);
            Wp.Extent extent1 = new Wp.Extent() { Cx = 6858000L, Cy = 76200L };
            Wp.EffectExtent effectExtent1 = new Wp.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L };
            Wp.WrapNone wrapNone1 = new Wp.WrapNone();
            Wp.DocProperties docProperties1 = new Wp.DocProperties() { Id = 1U, Name = "Shape 1" }; // Id and name should be unique for every shape
    
            Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
    
            A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks();
            graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
            nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
    
            A.Graphic graphic1 = new A.Graphic();
            graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
            A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape" };
    
            Wps.WordprocessingShape wordprocessingShape1 = new Wps.WordprocessingShape();
    
            Wps.NonVisualDrawingShapeProperties nonVisualDrawingShapeProperties1 = new Wps.NonVisualDrawingShapeProperties();
            A.ShapeLocks shapeLocks1 = new A.ShapeLocks();
    
            nonVisualDrawingShapeProperties1.Append(shapeLocks1);
    
            Wps.ShapeProperties shapeProperties1 = new Wps.ShapeProperties() { BlackWhiteMode = A.BlackWhiteModeValues.Auto };
    
            A.Transform2D transform2D1 = new A.Transform2D();
            A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
            A.Extents extents1 = new A.Extents() { Cx = 6858000L, Cy = 76200L };
    
            transform2D1.Append(offset1);
            transform2D1.Append(extents1);
    
            A.CustomGeometry customGeometry1 = new A.CustomGeometry();
            A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
            A.ShapeGuideList shapeGuideList1 = new A.ShapeGuideList();
            A.ShapeGuide shapeGuide1 = new A.ShapeGuide() { Name = "T0", Formula = "*/ 0 w 6858000" };
            A.ShapeGuide shapeGuide2 = new A.ShapeGuide() { Name = "T1", Formula = "*/ 0 h 76200" };
            A.ShapeGuide shapeGuide3 = new A.ShapeGuide() { Name = "T2", Formula = "*/ 0 w 6858000" };
            A.ShapeGuide shapeGuide4 = new A.ShapeGuide() { Name = "T3", Formula = "*/ 76200 h 76200" };
            A.ShapeGuide shapeGuide5 = new A.ShapeGuide() { Name = "T4", Formula = "*/ 6858000 w 6858000" };
            A.ShapeGuide shapeGuide6 = new A.ShapeGuide() { Name = "T5", Formula = "*/ 76200 h 76200" };
            A.ShapeGuide shapeGuide7 = new A.ShapeGuide() { Name = "T6", Formula = "*/ 6858000 w 6858000" };
            A.ShapeGuide shapeGuide8 = new A.ShapeGuide() { Name = "T7", Formula = "*/ 0 h 76200" };
            A.ShapeGuide shapeGuide9 = new A.ShapeGuide() { Name = "T8", Formula = "*/ 0 w 6858000" };
            A.ShapeGuide shapeGuide10 = new A.ShapeGuide() { Name = "T9", Formula = "*/ 0 h 76200" };
            A.ShapeGuide shapeGuide11 = new A.ShapeGuide() { Name = "T10", Formula = "*/ 0 60000 65536" };
            A.ShapeGuide shapeGuide12 = new A.ShapeGuide() { Name = "T11", Formula = "*/ 0 60000 65536" };
            A.ShapeGuide shapeGuide13 = new A.ShapeGuide() { Name = "T12", Formula = "*/ 0 60000 65536" };
            A.ShapeGuide shapeGuide14 = new A.ShapeGuide() { Name = "T13", Formula = "*/ 0 60000 65536" };
            A.ShapeGuide shapeGuide15 = new A.ShapeGuide() { Name = "T14", Formula = "*/ 0 60000 65536" };
            A.ShapeGuide shapeGuide16 = new A.ShapeGuide() { Name = "T15", Formula = "*/ 0 w 6858000" };
            A.ShapeGuide shapeGuide17 = new A.ShapeGuide() { Name = "T16", Formula = "*/ 0 h 76200" };
            A.ShapeGuide shapeGuide18 = new A.ShapeGuide() { Name = "T17", Formula = "*/ 6858000 w 6858000" };
            A.ShapeGuide shapeGuide19 = new A.ShapeGuide() { Name = "T18", Formula = "*/ 76200 h 76200" };
    
            shapeGuideList1.Append(shapeGuide1);
            shapeGuideList1.Append(shapeGuide2);
            shapeGuideList1.Append(shapeGuide3);
            shapeGuideList1.Append(shapeGuide4);
            shapeGuideList1.Append(shapeGuide5);
            shapeGuideList1.Append(shapeGuide6);
            shapeGuideList1.Append(shapeGuide7);
            shapeGuideList1.Append(shapeGuide8);
            shapeGuideList1.Append(shapeGuide9);
            shapeGuideList1.Append(shapeGuide10);
            shapeGuideList1.Append(shapeGuide11);
            shapeGuideList1.Append(shapeGuide12);
            shapeGuideList1.Append(shapeGuide13);
            shapeGuideList1.Append(shapeGuide14);
            shapeGuideList1.Append(shapeGuide15);
            shapeGuideList1.Append(shapeGuide16);
            shapeGuideList1.Append(shapeGuide17);
            shapeGuideList1.Append(shapeGuide18);
            shapeGuideList1.Append(shapeGuide19);
            A.AdjustHandleList adjustHandleList1 = new A.AdjustHandleList();
    
            A.ConnectionSiteList connectionSiteList1 = new A.ConnectionSiteList();
    
            A.ConnectionSite connectionSite1 = new A.ConnectionSite() { Angle = "T10" };
            A.Position position1 = new A.Position() { X = "T0", Y = "T1" };
    
            connectionSite1.Append(position1);
    
            A.ConnectionSite connectionSite2 = new A.ConnectionSite() { Angle = "T11" };
            A.Position position2 = new A.Position() { X = "T2", Y = "T3" };
    
            connectionSite2.Append(position2);
    
            A.ConnectionSite connectionSite3 = new A.ConnectionSite() { Angle = "T12" };
            A.Position position3 = new A.Position() { X = "T4", Y = "T5" };
    
            connectionSite3.Append(position3);
    
            A.ConnectionSite connectionSite4 = new A.ConnectionSite() { Angle = "T13" };
            A.Position position4 = new A.Position() { X = "T6", Y = "T7" };
    
            connectionSite4.Append(position4);
    
            A.ConnectionSite connectionSite5 = new A.ConnectionSite() { Angle = "T14" };
            A.Position position5 = new A.Position() { X = "T8", Y = "T9" };
    
            connectionSite5.Append(position5);
    
            connectionSiteList1.Append(connectionSite1);
            connectionSiteList1.Append(connectionSite2);
            connectionSiteList1.Append(connectionSite3);
            connectionSiteList1.Append(connectionSite4);
            connectionSiteList1.Append(connectionSite5);
            A.Rectangle rectangle1 = new A.Rectangle() { Left = "T15", Top = "T16", Right = "T17", Bottom = "T18" };
    
            A.PathList pathList1 = new A.PathList();
    
            A.Path path1 = new A.Path() { Width = 6858000L, Height = 76200L };
    
            A.MoveTo moveTo1 = new A.MoveTo();
            A.Point point1 = new A.Point() { X = "0", Y = "0" };
    
            moveTo1.Append(point1);
    
            A.LineTo lineTo1 = new A.LineTo();
            A.Point point2 = new A.Point() { X = "0", Y = "76200" };
    
            lineTo1.Append(point2);
    
            A.LineTo lineTo2 = new A.LineTo();
            A.Point point3 = new A.Point() { X = "6858000", Y = "76200" };
    
            lineTo2.Append(point3);
    
            A.LineTo lineTo3 = new A.LineTo();
            A.Point point4 = new A.Point() { X = "6858000", Y = "0" };
    
            lineTo3.Append(point4);
    
            A.LineTo lineTo4 = new A.LineTo();
            A.Point point5 = new A.Point() { X = "0", Y = "0" };
    
            lineTo4.Append(point5);
    
            path1.Append(moveTo1);
            path1.Append(lineTo1);
            path1.Append(lineTo2);
            path1.Append(lineTo3);
            path1.Append(lineTo4);
    
            pathList1.Append(path1);
    
            customGeometry1.Append(adjustValueList1);
            customGeometry1.Append(shapeGuideList1);
            customGeometry1.Append(adjustHandleList1);
            customGeometry1.Append(connectionSiteList1);
            customGeometry1.Append(rectangle1);
            customGeometry1.Append(pathList1);
    
            A.SolidFill solidFill1 = new A.SolidFill();
            A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00AFEF" };
    
            solidFill1.Append(rgbColorModelHex1);
    
            A.Outline outline1 = new A.Outline();
            A.NoFill noFill1 = new A.NoFill();
    
            outline1.Append(noFill1);
    
            A.ShapePropertiesExtensionList shapePropertiesExtensionList1 = new A.ShapePropertiesExtensionList();
    
            A.ShapePropertiesExtension shapePropertiesExtension1 = new A.ShapePropertiesExtension() { Uri = "{91240B29-F687-4F45-9708-019B960494DF}" };
    
            A14.HiddenLineProperties hiddenLineProperties1 = new A14.HiddenLineProperties() { Width = 9525 };
            hiddenLineProperties1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
    
            A.SolidFill solidFill2 = new A.SolidFill();
            A.RgbColorModelHex rgbColorModelHex2 = new A.RgbColorModelHex() { Val = "000000" };
    
            solidFill2.Append(rgbColorModelHex2);
            A.Round round1 = new A.Round();
            A.HeadEnd headEnd1 = new A.HeadEnd();
            A.TailEnd tailEnd1 = new A.TailEnd();
    
            hiddenLineProperties1.Append(solidFill2);
            hiddenLineProperties1.Append(round1);
            hiddenLineProperties1.Append(headEnd1);
            hiddenLineProperties1.Append(tailEnd1);
    
            shapePropertiesExtension1.Append(hiddenLineProperties1);
    
            shapePropertiesExtensionList1.Append(shapePropertiesExtension1);
    
            shapeProperties1.Append(transform2D1);
            shapeProperties1.Append(customGeometry1);
            shapeProperties1.Append(solidFill1);
            shapeProperties1.Append(outline1);
            shapeProperties1.Append(shapePropertiesExtensionList1);
    
            Wps.TextBodyProperties textBodyProperties1 = new Wps.TextBodyProperties() { Rotation = 0, Vertical = A.TextVerticalValues.Horizontal, Wrap = A.TextWrappingValues.Square, LeftInset = 91440, TopInset = 45720, RightInset = 91440, BottomInset = 45720, Anchor = A.TextAnchoringTypeValues.Top, AnchorCenter = false, UpRight = true };
            A.NoAutoFit noAutoFit1 = new A.NoAutoFit();
    
            textBodyProperties1.Append(noAutoFit1);
    
            wordprocessingShape1.Append(nonVisualDrawingShapeProperties1);
            wordprocessingShape1.Append(shapeProperties1);
            wordprocessingShape1.Append(textBodyProperties1);
    
            graphicData1.Append(wordprocessingShape1);
    
            graphic1.Append(graphicData1);
    
            Wp14.RelativeWidth relativeWidth1 = new Wp14.RelativeWidth() { ObjectId = Wp14.SizeRelativeHorizontallyValues.Page };
            Wp14.PercentageWidth percentageWidth1 = new Wp14.PercentageWidth();
            percentageWidth1.Text = "0";
    
            relativeWidth1.Append(percentageWidth1);
    
            Wp14.RelativeHeight relativeHeight1 = new Wp14.RelativeHeight() { RelativeFrom = Wp14.SizeRelativeVerticallyValues.Page };
            Wp14.PercentageHeight percentageHeight1 = new Wp14.PercentageHeight();
            percentageHeight1.Text = "0";
    
            relativeHeight1.Append(percentageHeight1);
    
            anchor1.Append(simplePosition1);
            anchor1.Append(horizontalPosition1);
            anchor1.Append(verticalPosition1);
            anchor1.Append(extent1);
            anchor1.Append(effectExtent1);
            anchor1.Append(wrapNone1);
            anchor1.Append(docProperties1);
            anchor1.Append(nonVisualGraphicFrameDrawingProperties1);
            anchor1.Append(graphic1);
            anchor1.Append(relativeWidth1);
            anchor1.Append(relativeHeight1);
    
            drawing1.Append(anchor1);
    
            alternateContentChoice1.Append(drawing1);
    
            AlternateContentFallback alternateContentFallback1 = new AlternateContentFallback();
    
            Picture picture1 = new Picture();
            //Use Id same as what you used in DocProperties 
            V.Shape shape1 = new V.Shape() { Id = "Shape 1", Style = "position:absolute;margin-left:0;margin-top:63pt;width:540pt;height:6pt;z-index:-251658240;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:0;mso-wrap-distance-top:0;mso-wrap-distance-right:0;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:page;mso-position-vertical:absolute;mso-position-vertical-relative:page;mso-width-percent:0;mso-height-percent:0;mso-width-relative:page;mso-height-relative:page;v-text-anchor:top", CoordinateSize = "6858000,76200", OptionalString = "_x0000_s1026", AllowInCell = false, FillColor = "#00afef", Stroked = false, Edg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\nAAYACAAAACEAu18l7d0AAAAJAQAADwAAAGRycy9kb3ducmV2LnhtbEyPzU7DMBCE70i8g7VI3KhN\nkaoQ4lSlEkKIip/SB3DjbRI1XhvbbcPbsz3BbXZnNftNNR/dII4YU+9Jw+1EgUBqvO2p1bD5erop\nQKRsyJrBE2r4wQTz+vKiMqX1J/rE4zq3gkMolUZDl3MopUxNh86kiQ9I7O18dCbzGFtpozlxuBvk\nVKmZdKYn/tCZgMsOm/364DR8rz7sZnx/Xvn78Lh8Ca+yjW87ra+vxsUDiIxj/juGMz6jQ81MW38g\nm8SggYtk3k5nLM62KhSrLau7QoGsK/m/Qf0LAAD//wMAUEsBAi0AFAAGAAgAAAAhALaDOJL+AAAA\n4QEAABMAAAAAAAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECLQAUAAYACAAAACEA\nOP0h/9YAAACUAQAACwAAAAAAAAAAAAAAAAAvAQAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEA\nusown6QDAAAoCgAADgAAAAAAAAAAAAAAAAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAA\nACEAu18l7d0AAAAJAQAADwAAAAAAAAAAAAAAAAD+BQAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAE\nAAQA8wAAAAgHAAAAAA==\n" };
    
            shape1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
            shape1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            shape1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            shape1.SetAttribute(new OpenXmlAttribute("w14", "anchorId", "http://schemas.microsoft.com/office/word/2010/wordml", anchor1.AnchorId));//Set anchorId attribute value same as anchorID
            V.Path path2 = new V.Path() { TextboxRectangle = "0,0,6858000,76200", ShowArrowhead = true, ConnectionPointType = Ovml.ConnectValues.Custom, ConnectionPoints = "0,0;0,76200;6858000,76200;6858000,0;0,0", ConnectAngles = "0,0,0,0,0" };
            Wvml.TextWrap textWrap1 = new Wvml.TextWrap() { AnchorX = Wvml.HorizontalAnchorValues.Page, AnchorY = Wvml.VerticalAnchorValues.Page };
    
            shape1.Append(path2);
            shape1.Append(textWrap1);
    
            picture1.Append(shape1);
    
            alternateContentFallback1.Append(picture1);
    
            alternateContent1.Append(alternateContentChoice1);
            alternateContent1.Append(alternateContentFallback1);
    
            run1.Append(runProperties1);
            run1.Append(alternateContent1);
            return run1;
           }
           public string GetRandomHexNumber(int digits)
           {
                byte[] buffer = new byte[digits / 2];
                random.NextBytes(buffer);
                string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());
                if (digits % 2 == 0)
                    return result;
                return result + random.Next(16).ToString("X");
           }
    

    Add this Run object into your Paragraph object of OpenXML.