none
Create a new shape in the slide master RRS feed

  • Question

  • I am having trouble appending a shape to a slide master. I am trying to insert a textbox with some text in the slide master, like a footer. I am very new to openxml and I was wondering if someone can help me. When I use my code below, I end up corrupting my presentation file. Any help is very much appreciated:

    PresentationPart

    SlideMasterPart smPart = (SlideMasterPart)presentationPart.GetPartById("rId1");

    CommonSlideData sldData = smPart.SlideMaster.CommonSlideData;

    ShapeTree shapeTree = (ShapeTree)sldData.ShapeTree;

    Shape shape = new Shape();

    NonVisualShapeProperties nonVisualShapeProperties = new NonVisualShapeProperties() ;

    NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "Footer" };

    NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties = new NonVisualShapeDrawingProperties();

    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();

    nonVisualShapeProperties.Append(nonVisualDrawingProperties);

    nonVisualShapeProperties.Append(nonVisualShapeDrawingProperties);

    nonVisualShapeProperties.Append(applicationNonVisualDrawingProperties);

     

    TextBody textBody = new TextBody();Paragraph paragraph = new A.Paragraph();

    A.

    ParagraphProperties paragraphProperties = new A.ParagraphProperties() { DefaultTabSize = 1018167 };

    A.

    DefaultRunProperties defaultRunProperties = new A.DefaultRunProperties();

    paragraphProperties.Append(defaultRunProperties);

    A.

    Run run = new A.Run();

    A.

    RunProperties runProperties = new A.RunProperties() { Language = "en-US", FontSize = 700, Dirty = false };

    runProperties.SetAttribute(

    new OpenXmlAttribute("", "smtClean", "", "0"));Text text = new A.Text();

    text.Text =

    "© 2014 XYZ Limited-------------";

    run.Append(runProperties);

    run.Append(text);

    paragraph.Append(paragraphProperties);

    paragraph.Append(run);

    textBody.Append(paragraph);

    shape.Append(textBody);

    shapeTree.Append(shape);

    presentationPart.Presentation.Save();

    A.

    A.

    presentationPart = pptDoc.PresentationPart;
    • Edited by SSule Thursday, June 26, 2014 4:25 AM
    Thursday, June 26, 2014 4:24 AM

Answers

  • Hi,

    A easy way is to add the shape manually (refer to Create and customize a slide master) then view the XML elements in Open XML SDK tool.

    For example I added a shape as below:

    I got code like this:

    // Creates an Shape instance and adds its children.
            public static Shape GenerateShape()
            {
                Shape shape1 = new Shape();
    
                NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)7U, Name = "Slide Number Placeholder 5" };
    
                NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new NonVisualShapeDrawingProperties() { TextBox = true };
                A.ShapeLocks shapeLocks1 = new A.ShapeLocks();
    
                nonVisualShapeDrawingProperties1.Append(shapeLocks1);
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties() { UserDrawn = true };
    
                nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
                nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
                nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);
    
                ShapeProperties shapeProperties1 = new ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset() { X = 4591684L, Y = 4135416L };
                A.Extents extents1 = new A.Extents() { Cx = 2743200L, Cy = 365125L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                TextBody textBody1 = new TextBody();
                A.BodyProperties bodyProperties1 = new A.BodyProperties() { Vertical = A.TextVerticalValues.Horizontal, LeftInset = 91440, TopInset = 45720, RightInset = 91440, BottomInset = 45720, RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Center };
    
                A.ListStyle listStyle1 = new A.ListStyle();
    
                A.DefaultParagraphProperties defaultParagraphProperties1 = new A.DefaultParagraphProperties();
                A.DefaultRunProperties defaultRunProperties1 = new A.DefaultRunProperties() { Language = "en-US" };
    
                defaultParagraphProperties1.Append(defaultRunProperties1);
    
                A.Level1ParagraphProperties level1ParagraphProperties1 = new A.Level1ParagraphProperties() { LeftMargin = 0, Alignment = A.TextAlignmentTypeValues.Right, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties2 = new A.DefaultRunProperties() { FontSize = 1200, Kerning = 1200 };
    
                A.SolidFill solidFill1 = new A.SolidFill();
    
                A.SchemeColor schemeColor1 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
                A.Tint tint1 = new A.Tint() { Val = 75000 };
    
                schemeColor1.Append(tint1);
    
                solidFill1.Append(schemeColor1);
                A.LatinFont latinFont1 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont1 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont1 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties2.Append(solidFill1);
                defaultRunProperties2.Append(latinFont1);
                defaultRunProperties2.Append(eastAsianFont1);
                defaultRunProperties2.Append(complexScriptFont1);
    
                level1ParagraphProperties1.Append(defaultRunProperties2);
    
                A.Level2ParagraphProperties level2ParagraphProperties1 = new A.Level2ParagraphProperties() { LeftMargin = 457200, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties3 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill2 = new A.SolidFill();
                A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill2.Append(schemeColor2);
                A.LatinFont latinFont2 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont2 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont2 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties3.Append(solidFill2);
                defaultRunProperties3.Append(latinFont2);
                defaultRunProperties3.Append(eastAsianFont2);
                defaultRunProperties3.Append(complexScriptFont2);
    
                level2ParagraphProperties1.Append(defaultRunProperties3);
    
                A.Level3ParagraphProperties level3ParagraphProperties1 = new A.Level3ParagraphProperties() { LeftMargin = 914400, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties4 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill3 = new A.SolidFill();
                A.SchemeColor schemeColor3 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill3.Append(schemeColor3);
                A.LatinFont latinFont3 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont3 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont3 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties4.Append(solidFill3);
                defaultRunProperties4.Append(latinFont3);
                defaultRunProperties4.Append(eastAsianFont3);
                defaultRunProperties4.Append(complexScriptFont3);
    
                level3ParagraphProperties1.Append(defaultRunProperties4);
    
                A.Level4ParagraphProperties level4ParagraphProperties1 = new A.Level4ParagraphProperties() { LeftMargin = 1371600, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties5 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill4 = new A.SolidFill();
                A.SchemeColor schemeColor4 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill4.Append(schemeColor4);
                A.LatinFont latinFont4 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont4 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont4 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties5.Append(solidFill4);
                defaultRunProperties5.Append(latinFont4);
                defaultRunProperties5.Append(eastAsianFont4);
                defaultRunProperties5.Append(complexScriptFont4);
    
                level4ParagraphProperties1.Append(defaultRunProperties5);
    
                A.Level5ParagraphProperties level5ParagraphProperties1 = new A.Level5ParagraphProperties() { LeftMargin = 1828800, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties6 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill5 = new A.SolidFill();
                A.SchemeColor schemeColor5 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill5.Append(schemeColor5);
                A.LatinFont latinFont5 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont5 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont5 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties6.Append(solidFill5);
                defaultRunProperties6.Append(latinFont5);
                defaultRunProperties6.Append(eastAsianFont5);
                defaultRunProperties6.Append(complexScriptFont5);
    
                level5ParagraphProperties1.Append(defaultRunProperties6);
    
                A.Level6ParagraphProperties level6ParagraphProperties1 = new A.Level6ParagraphProperties() { LeftMargin = 2286000, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties7 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill6 = new A.SolidFill();
                A.SchemeColor schemeColor6 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill6.Append(schemeColor6);
                A.LatinFont latinFont6 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont6 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont6 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties7.Append(solidFill6);
                defaultRunProperties7.Append(latinFont6);
                defaultRunProperties7.Append(eastAsianFont6);
                defaultRunProperties7.Append(complexScriptFont6);
    
                level6ParagraphProperties1.Append(defaultRunProperties7);
    
                A.Level7ParagraphProperties level7ParagraphProperties1 = new A.Level7ParagraphProperties() { LeftMargin = 2743200, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties8 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill7 = new A.SolidFill();
                A.SchemeColor schemeColor7 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill7.Append(schemeColor7);
                A.LatinFont latinFont7 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont7 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont7 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties8.Append(solidFill7);
                defaultRunProperties8.Append(latinFont7);
                defaultRunProperties8.Append(eastAsianFont7);
                defaultRunProperties8.Append(complexScriptFont7);
    
                level7ParagraphProperties1.Append(defaultRunProperties8);
    
                A.Level8ParagraphProperties level8ParagraphProperties1 = new A.Level8ParagraphProperties() { LeftMargin = 3200400, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties9 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill8 = new A.SolidFill();
                A.SchemeColor schemeColor8 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill8.Append(schemeColor8);
                A.LatinFont latinFont8 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont8 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont8 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties9.Append(solidFill8);
                defaultRunProperties9.Append(latinFont8);
                defaultRunProperties9.Append(eastAsianFont8);
                defaultRunProperties9.Append(complexScriptFont8);
    
                level8ParagraphProperties1.Append(defaultRunProperties9);
    
                A.Level9ParagraphProperties level9ParagraphProperties1 = new A.Level9ParagraphProperties() { LeftMargin = 3657600, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties10 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill9 = new A.SolidFill();
                A.SchemeColor schemeColor9 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill9.Append(schemeColor9);
                A.LatinFont latinFont9 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont9 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont9 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties10.Append(solidFill9);
                defaultRunProperties10.Append(latinFont9);
                defaultRunProperties10.Append(eastAsianFont9);
                defaultRunProperties10.Append(complexScriptFont9);
    
                level9ParagraphProperties1.Append(defaultRunProperties10);
    
                listStyle1.Append(defaultParagraphProperties1);
                listStyle1.Append(level1ParagraphProperties1);
                listStyle1.Append(level2ParagraphProperties1);
                listStyle1.Append(level3ParagraphProperties1);
                listStyle1.Append(level4ParagraphProperties1);
                listStyle1.Append(level5ParagraphProperties1);
                listStyle1.Append(level6ParagraphProperties1);
                listStyle1.Append(level7ParagraphProperties1);
                listStyle1.Append(level8ParagraphProperties1);
                listStyle1.Append(level9ParagraphProperties1);
    
                A.Paragraph paragraph1 = new A.Paragraph();
    
                A.Run run1 = new A.Run();
    
                A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", Dirty = false };
                runProperties1.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
                A.Text text1 = new A.Text();
                text1.Text = "My custom shape";
    
                run1.Append(runProperties1);
                run1.Append(text1);
                A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false };
    
                paragraph1.Append(run1);
                paragraph1.Append(endParagraphRunProperties1);
    
                textBody1.Append(bodyProperties1);
                textBody1.Append(listStyle1);
                textBody1.Append(paragraph1);
    
                shape1.Append(nonVisualShapeProperties1);
                shape1.Append(shapeProperties1);
                shape1.Append(textBody1);
                return shape1;
            }

    Now if you want to add this shape into a slide layout, please try:

    public static void CreatePresentation(string filepath)
            {
                // Create a presentation at a specified file path. The presentation document type is pptx, by default.
                using (PresentationDocument presentationDoc = PresentationDocument.Open(filepath, true))
                {
                    PresentationPart presentationPart = presentationDoc.PresentationPart;
                    SlideMasterPart smPart = (SlideMasterPart)presentationPart.GetPartById("rId1");
                    
                    SlideLayoutPart slPart = (SlideLayoutPart)smPart.GetPartById("rId1");
                    CommonSlideData sldData = slPart.SlideLayout.CommonSlideData;
                    ShapeTree shapeTree = (ShapeTree)sldData.ShapeTree;
                    shapeTree.Append(GenerateShape());
                }
            }

    If you want to add this shape into all layouts, you could insert a shape under:

    Please try:

    public static void CreatePresentation(string filepath)
            {
                // Create a presentation at a specified file path. The presentation document type is pptx, by default.
                using (PresentationDocument presentationDoc = PresentationDocument.Open(filepath, true))
                {
                    PresentationPart presentationPart = presentationDoc.PresentationPart;
                    SlideMasterPart smPart = (SlideMasterPart)presentationPart.GetPartById("rId1");
                    CommonSlideData sldData = smPart.SlideMaster.CommonSlideData;
                    ShapeTree shapeTree = (ShapeTree)sldData.ShapeTree;
    
                    shapeTree.Append(GenerateShape());
                }
            }

    I have tested these code in PowerPoint 2013, it works fine for me.

    Hope this helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 30, 2014 9:18 AM
    Moderator

All replies

  • Hi,

    The structure of slide master is like this:

    You need to find the specified SlideMaster by id and insert a new Shape under ShapeTree elements.

    To create a new shape, you could refer to the reflected code through OpenXML SDK tool. Here is the code I got:

    using DocumentFormat.OpenXml.Presentation;
    using DocumentFormat.OpenXml;
    using A = DocumentFormat.OpenXml.Drawing;
    namespace GeneratedCode
    {
        public class GeneratedClass
        {
            // Creates an Shape instance and adds its children.
            public Shape GenerateShape()
            {
                Shape shape1 = new Shape();
    
                NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties(){ Id = (UInt32Value)2U, Name = "Title 1" };
    
                NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new NonVisualShapeDrawingProperties();
                A.ShapeLocks shapeLocks1 = new A.ShapeLocks(){ NoGrouping = true };
    
                nonVisualShapeDrawingProperties1.Append(shapeLocks1);
    
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();
                PlaceholderShape placeholderShape1 = new PlaceholderShape(){ Type = PlaceholderValues.CenteredTitle };
    
                applicationNonVisualDrawingProperties1.Append(placeholderShape1);
    
                nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
                nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
                nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);
    
                ShapeProperties shapeProperties1 = new ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset(){ X = 1154955L, Y = 1447800L };
                A.Extents extents1 = new A.Extents(){ Cx = 8825658L, Cy = 3329581L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                shapeProperties1.Append(transform2D1);
    
                TextBody textBody1 = new TextBody();
                A.BodyProperties bodyProperties1 = new A.BodyProperties(){ Anchor = A.TextAnchoringTypeValues.Bottom };
    
                A.ListStyle listStyle1 = new A.ListStyle();
    
                A.Level1ParagraphProperties level1ParagraphProperties1 = new A.Level1ParagraphProperties();
                A.DefaultRunProperties defaultRunProperties1 = new A.DefaultRunProperties(){ FontSize = 7200 };
    
                level1ParagraphProperties1.Append(defaultRunProperties1);
    
                listStyle1.Append(level1ParagraphProperties1);
    
                A.Paragraph paragraph1 = new A.Paragraph();
    
                A.Run run1 = new A.Run();
    
                A.RunProperties runProperties1 = new A.RunProperties(){ Language = "en-US" };
                runProperties1.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
                A.Text text1 = new A.Text();
                text1.Text = "Click to edit Master title style";
    
                run1.Append(runProperties1);
                run1.Append(text1);
                A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties(){ Language = "en-US", Dirty = false };
    
                paragraph1.Append(run1);
                paragraph1.Append(endParagraphRunProperties1);
    
                textBody1.Append(bodyProperties1);
                textBody1.Append(listStyle1);
                textBody1.Append(paragraph1);
    
                shape1.Append(nonVisualShapeProperties1);
                shape1.Append(shapeProperties1);
                shape1.Append(textBody1);
                return shape1;
            }
    
    
        }
    }

    You only need to replace the text "Click to edit Master title style".

    Hope this helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 27, 2014 9:01 AM
    Moderator
  • Hi George,

    Thank you so much for your response. From my original post, I have progressed and have updated the slide master with my shape which is quite to what you have in your email. But for some reason, the text doesnt seem to show up on my slides.

    Also you showed the commonslidedata under slide layouts, I am guessing for me to have the text show up on all the slides, i need to update the slide master?

    Any ideas of anything else that I should be doing?

    Regards,

    SS


    • Edited by SSule Sunday, June 29, 2014 9:48 AM typos
    Friday, June 27, 2014 9:53 AM
  • Hi,

    A easy way is to add the shape manually (refer to Create and customize a slide master) then view the XML elements in Open XML SDK tool.

    For example I added a shape as below:

    I got code like this:

    // Creates an Shape instance and adds its children.
            public static Shape GenerateShape()
            {
                Shape shape1 = new Shape();
    
                NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)7U, Name = "Slide Number Placeholder 5" };
    
                NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new NonVisualShapeDrawingProperties() { TextBox = true };
                A.ShapeLocks shapeLocks1 = new A.ShapeLocks();
    
                nonVisualShapeDrawingProperties1.Append(shapeLocks1);
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties() { UserDrawn = true };
    
                nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
                nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
                nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);
    
                ShapeProperties shapeProperties1 = new ShapeProperties();
    
                A.Transform2D transform2D1 = new A.Transform2D();
                A.Offset offset1 = new A.Offset() { X = 4591684L, Y = 4135416L };
                A.Extents extents1 = new A.Extents() { Cx = 2743200L, Cy = 365125L };
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
                A.AdjustValueList adjustValueList1 = new A.AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                TextBody textBody1 = new TextBody();
                A.BodyProperties bodyProperties1 = new A.BodyProperties() { Vertical = A.TextVerticalValues.Horizontal, LeftInset = 91440, TopInset = 45720, RightInset = 91440, BottomInset = 45720, RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Center };
    
                A.ListStyle listStyle1 = new A.ListStyle();
    
                A.DefaultParagraphProperties defaultParagraphProperties1 = new A.DefaultParagraphProperties();
                A.DefaultRunProperties defaultRunProperties1 = new A.DefaultRunProperties() { Language = "en-US" };
    
                defaultParagraphProperties1.Append(defaultRunProperties1);
    
                A.Level1ParagraphProperties level1ParagraphProperties1 = new A.Level1ParagraphProperties() { LeftMargin = 0, Alignment = A.TextAlignmentTypeValues.Right, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties2 = new A.DefaultRunProperties() { FontSize = 1200, Kerning = 1200 };
    
                A.SolidFill solidFill1 = new A.SolidFill();
    
                A.SchemeColor schemeColor1 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
                A.Tint tint1 = new A.Tint() { Val = 75000 };
    
                schemeColor1.Append(tint1);
    
                solidFill1.Append(schemeColor1);
                A.LatinFont latinFont1 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont1 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont1 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties2.Append(solidFill1);
                defaultRunProperties2.Append(latinFont1);
                defaultRunProperties2.Append(eastAsianFont1);
                defaultRunProperties2.Append(complexScriptFont1);
    
                level1ParagraphProperties1.Append(defaultRunProperties2);
    
                A.Level2ParagraphProperties level2ParagraphProperties1 = new A.Level2ParagraphProperties() { LeftMargin = 457200, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties3 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill2 = new A.SolidFill();
                A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill2.Append(schemeColor2);
                A.LatinFont latinFont2 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont2 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont2 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties3.Append(solidFill2);
                defaultRunProperties3.Append(latinFont2);
                defaultRunProperties3.Append(eastAsianFont2);
                defaultRunProperties3.Append(complexScriptFont2);
    
                level2ParagraphProperties1.Append(defaultRunProperties3);
    
                A.Level3ParagraphProperties level3ParagraphProperties1 = new A.Level3ParagraphProperties() { LeftMargin = 914400, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties4 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill3 = new A.SolidFill();
                A.SchemeColor schemeColor3 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill3.Append(schemeColor3);
                A.LatinFont latinFont3 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont3 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont3 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties4.Append(solidFill3);
                defaultRunProperties4.Append(latinFont3);
                defaultRunProperties4.Append(eastAsianFont3);
                defaultRunProperties4.Append(complexScriptFont3);
    
                level3ParagraphProperties1.Append(defaultRunProperties4);
    
                A.Level4ParagraphProperties level4ParagraphProperties1 = new A.Level4ParagraphProperties() { LeftMargin = 1371600, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties5 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill4 = new A.SolidFill();
                A.SchemeColor schemeColor4 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill4.Append(schemeColor4);
                A.LatinFont latinFont4 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont4 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont4 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties5.Append(solidFill4);
                defaultRunProperties5.Append(latinFont4);
                defaultRunProperties5.Append(eastAsianFont4);
                defaultRunProperties5.Append(complexScriptFont4);
    
                level4ParagraphProperties1.Append(defaultRunProperties5);
    
                A.Level5ParagraphProperties level5ParagraphProperties1 = new A.Level5ParagraphProperties() { LeftMargin = 1828800, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties6 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill5 = new A.SolidFill();
                A.SchemeColor schemeColor5 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill5.Append(schemeColor5);
                A.LatinFont latinFont5 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont5 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont5 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties6.Append(solidFill5);
                defaultRunProperties6.Append(latinFont5);
                defaultRunProperties6.Append(eastAsianFont5);
                defaultRunProperties6.Append(complexScriptFont5);
    
                level5ParagraphProperties1.Append(defaultRunProperties6);
    
                A.Level6ParagraphProperties level6ParagraphProperties1 = new A.Level6ParagraphProperties() { LeftMargin = 2286000, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties7 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill6 = new A.SolidFill();
                A.SchemeColor schemeColor6 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill6.Append(schemeColor6);
                A.LatinFont latinFont6 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont6 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont6 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties7.Append(solidFill6);
                defaultRunProperties7.Append(latinFont6);
                defaultRunProperties7.Append(eastAsianFont6);
                defaultRunProperties7.Append(complexScriptFont6);
    
                level6ParagraphProperties1.Append(defaultRunProperties7);
    
                A.Level7ParagraphProperties level7ParagraphProperties1 = new A.Level7ParagraphProperties() { LeftMargin = 2743200, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties8 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill7 = new A.SolidFill();
                A.SchemeColor schemeColor7 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill7.Append(schemeColor7);
                A.LatinFont latinFont7 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont7 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont7 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties8.Append(solidFill7);
                defaultRunProperties8.Append(latinFont7);
                defaultRunProperties8.Append(eastAsianFont7);
                defaultRunProperties8.Append(complexScriptFont7);
    
                level7ParagraphProperties1.Append(defaultRunProperties8);
    
                A.Level8ParagraphProperties level8ParagraphProperties1 = new A.Level8ParagraphProperties() { LeftMargin = 3200400, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties9 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill8 = new A.SolidFill();
                A.SchemeColor schemeColor8 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill8.Append(schemeColor8);
                A.LatinFont latinFont8 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont8 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont8 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties9.Append(solidFill8);
                defaultRunProperties9.Append(latinFont8);
                defaultRunProperties9.Append(eastAsianFont8);
                defaultRunProperties9.Append(complexScriptFont8);
    
                level8ParagraphProperties1.Append(defaultRunProperties9);
    
                A.Level9ParagraphProperties level9ParagraphProperties1 = new A.Level9ParagraphProperties() { LeftMargin = 3657600, Alignment = A.TextAlignmentTypeValues.Left, DefaultTabSize = 914400, RightToLeft = false, EastAsianLineBreak = true, LatinLineBreak = false, Height = true };
    
                A.DefaultRunProperties defaultRunProperties10 = new A.DefaultRunProperties() { FontSize = 1800, Kerning = 1200 };
    
                A.SolidFill solidFill9 = new A.SolidFill();
                A.SchemeColor schemeColor9 = new A.SchemeColor() { Val = A.SchemeColorValues.Text1 };
    
                solidFill9.Append(schemeColor9);
                A.LatinFont latinFont9 = new A.LatinFont() { Typeface = "+mn-lt" };
                A.EastAsianFont eastAsianFont9 = new A.EastAsianFont() { Typeface = "+mn-ea" };
                A.ComplexScriptFont complexScriptFont9 = new A.ComplexScriptFont() { Typeface = "+mn-cs" };
    
                defaultRunProperties10.Append(solidFill9);
                defaultRunProperties10.Append(latinFont9);
                defaultRunProperties10.Append(eastAsianFont9);
                defaultRunProperties10.Append(complexScriptFont9);
    
                level9ParagraphProperties1.Append(defaultRunProperties10);
    
                listStyle1.Append(defaultParagraphProperties1);
                listStyle1.Append(level1ParagraphProperties1);
                listStyle1.Append(level2ParagraphProperties1);
                listStyle1.Append(level3ParagraphProperties1);
                listStyle1.Append(level4ParagraphProperties1);
                listStyle1.Append(level5ParagraphProperties1);
                listStyle1.Append(level6ParagraphProperties1);
                listStyle1.Append(level7ParagraphProperties1);
                listStyle1.Append(level8ParagraphProperties1);
                listStyle1.Append(level9ParagraphProperties1);
    
                A.Paragraph paragraph1 = new A.Paragraph();
    
                A.Run run1 = new A.Run();
    
                A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", Dirty = false };
                runProperties1.SetAttribute(new OpenXmlAttribute("", "smtClean", "", "0"));
                A.Text text1 = new A.Text();
                text1.Text = "My custom shape";
    
                run1.Append(runProperties1);
                run1.Append(text1);
                A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false };
    
                paragraph1.Append(run1);
                paragraph1.Append(endParagraphRunProperties1);
    
                textBody1.Append(bodyProperties1);
                textBody1.Append(listStyle1);
                textBody1.Append(paragraph1);
    
                shape1.Append(nonVisualShapeProperties1);
                shape1.Append(shapeProperties1);
                shape1.Append(textBody1);
                return shape1;
            }

    Now if you want to add this shape into a slide layout, please try:

    public static void CreatePresentation(string filepath)
            {
                // Create a presentation at a specified file path. The presentation document type is pptx, by default.
                using (PresentationDocument presentationDoc = PresentationDocument.Open(filepath, true))
                {
                    PresentationPart presentationPart = presentationDoc.PresentationPart;
                    SlideMasterPart smPart = (SlideMasterPart)presentationPart.GetPartById("rId1");
                    
                    SlideLayoutPart slPart = (SlideLayoutPart)smPart.GetPartById("rId1");
                    CommonSlideData sldData = slPart.SlideLayout.CommonSlideData;
                    ShapeTree shapeTree = (ShapeTree)sldData.ShapeTree;
                    shapeTree.Append(GenerateShape());
                }
            }

    If you want to add this shape into all layouts, you could insert a shape under:

    Please try:

    public static void CreatePresentation(string filepath)
            {
                // Create a presentation at a specified file path. The presentation document type is pptx, by default.
                using (PresentationDocument presentationDoc = PresentationDocument.Open(filepath, true))
                {
                    PresentationPart presentationPart = presentationDoc.PresentationPart;
                    SlideMasterPart smPart = (SlideMasterPart)presentationPart.GetPartById("rId1");
                    CommonSlideData sldData = smPart.SlideMaster.CommonSlideData;
                    ShapeTree shapeTree = (ShapeTree)sldData.ShapeTree;
    
                    shapeTree.Append(GenerateShape());
                }
            }

    I have tested these code in PowerPoint 2013, it works fine for me.

    Hope this helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 30, 2014 9:18 AM
    Moderator