How to embed textbox into powerpoint using openxml?

qiangang 40 Reputation points
2023-03-14T08:11:11.6766667+00:00

I am trying to embed textbox into slide,but my pragram does not work. I am looking forward your help. This is my code.

public void AddShape(Slide iSlide, long x, long y, long width, long heigth, D.ShapeTypeValues shapeType, string rgbColorHex)
        {
            Shape shape = new Shape();
            ShapeStyle shapeStyle = CreateShapeStyle();
            ShapeProperties shapeProperties = CreateShapeProperties(x, y, width, heigth, shapeType, rgbColorHex);
            TextBody textBody = CreateTextBody();
            NonVisualShapeProperties nonVisualShapeProperties = CreateNonVisualShapeProperties();
            shape.Append(nonVisualShapeProperties);
            shape.Append(shapeProperties);
            shape.Append(shapeStyle);
            shape.Append(textBody);
            iSlide.CommonSlideData.ShapeTree.Append(shape);
            
        }
        private ShapeStyle CreateShapeStyle()
        {
            ShapeStyle shapeStyle = new ShapeStyle();

            D.LineReference lineReference = new D.LineReference() { Index = (UInt32Value)2U };

            D.SchemeColor schemeColor2 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };
            D.Shade shade = new D.Shade() { Val = 50000 };

            schemeColor2.Append(shade);

            lineReference.Append(schemeColor2);

            D.FillReference fillReference = new D.FillReference() { Index = (UInt32Value)1U };
            D.SchemeColor schemeColor3 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };

            fillReference.Append(schemeColor3);

            D.EffectReference effectReference = new D.EffectReference() { Index = (UInt32Value)0U };
            D.SchemeColor schemeColor4 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };

            effectReference.Append(schemeColor4);

            D.FontReference fontReference = new D.FontReference() { Index = D.FontCollectionIndexValues.Minor };
            D.SchemeColor schemeColor5 = new D.SchemeColor() { Val = D.SchemeColorValues.Light1 };

            fontReference.Append(schemeColor5);

            shapeStyle.Append(lineReference);
            shapeStyle.Append(fillReference);
            shapeStyle.Append(effectReference);
            shapeStyle.Append(fontReference);
            return shapeStyle;
        }
        private ShapeProperties CreateShapeProperties(long x, long y, long width, long heigth, D.ShapeTypeValues shapeType, string rgbColorHex)
        {
            ShapeProperties shapeProperties = new ShapeProperties();
            D.Transform2D transform2D = new D.Transform2D();
            D.Offset offset = new D.Offset() { X = x, Y = y };
            D.Extents extents = new D.Extents() { Cx = width, Cy = heigth };
            transform2D.Append(offset);
            transform2D.Append(extents);

            D.PresetGeometry presetGeometry = new D.PresetGeometry() { Preset = shapeType };
            D.AdjustValueList adjustValueList = new D.AdjustValueList();

            presetGeometry.Append(adjustValueList);

            D.SolidFill solidFill = new D.SolidFill();
            D.RgbColorModelHex rgbColorModelHex = new D.RgbColorModelHex() { Val = rgbColorHex };

            solidFill.Append(rgbColorModelHex);

            D.Outline outline = new D.Outline() { Width = 2700 };

            D.SolidFill solidFill2 = new D.SolidFill();
            D.SchemeColor schemeColor = new D.SchemeColor() { Val = D.SchemeColorValues.Text1 };

            solidFill2.Append(schemeColor);

            outline.Append(solidFill2);

            shapeProperties.Append(transform2D);
            shapeProperties.Append(presetGeometry);
            shapeProperties.Append(solidFill);
            shapeProperties.Append(outline);
            return shapeProperties;
        }
        private TextBody CreateTextBody()
        {
            TextBody textBody = new TextBody();
            D.BodyProperties bodyProperties = new D.BodyProperties() { RightToLeftColumns = false, Anchor = D.TextAnchoringTypeValues.Center };
            D.ListStyle listStyle = new D.ListStyle();

            D.Paragraph paragraph = new D.Paragraph();
            D.ParagraphProperties paragraphProperties = new D.ParagraphProperties() { Alignment = D.TextAlignmentTypeValues.Center };
            D.EndParagraphRunProperties endParagraphRunProperties = new D.EndParagraphRunProperties() { Language = "es-ES" };

            paragraph.Append(paragraphProperties);
            paragraph.Append(endParagraphRunProperties);
            
            textBody.Append(bodyProperties);
            textBody.Append(listStyle);
            textBody.Append(paragraph);
            
            return textBody;
        }
        private NonVisualShapeProperties CreateNonVisualShapeProperties()
        {
            NonVisualShapeProperties nonVisualShapeProperties = new NonVisualShapeProperties();
            NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = " Shape Name" };
            NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties = new NonVisualShapeDrawingProperties();
            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();

            nonVisualShapeProperties.Append(nonVisualDrawingProperties);
            nonVisualShapeProperties.Append(nonVisualShapeDrawingProperties);
            nonVisualShapeProperties.Append(applicationNonVisualDrawingProperties);
            return nonVisualShapeProperties;
        }
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,459 questions
{count} votes