wordpress hit counter
create new slide after the second slide and create Text in PPtx file - .Net - Development Tools - OpenXML Developer

create new slide after the second slide and create Text in PPtx file

Development Tools

Discussions about working with Open XML using a wide range of development tools

create new slide after the second slide and create Text in PPtx file

  • This post has 12 Replies |
  • 4 Followers
  • Hi,

    I am trying to add a new image to a newly created slide in a PowerPoint presentation its working fine(i used pramodhegde code). but the new slide creating last i have to create in after second slide and if again i created new slide mean it have create after the third slide....then i have to create text in ppt and i have to pass textbox values in that text.....

    Can anyone help me in this regard?

    I am doing it using Asp.Net with C# and OpenXML SDK.

    Any help? Thanks in anticipation

  • Hi,

        You can add the slide in whichever position you like. Use the following code to insert the slide in an absolute position.

    internal static void InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)       
    {           

                int slideInsertedPostion = 0;            

                UInt32 slideId = 256U;           
               slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count());
                Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
                SlidePart sPart = presentationPart.AddNewPart<SlidePart>();           
                slide.Save(sPart);
                SlideMasterPart smPart = presentationPart.SlideMasterParts.First();           
                SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName));
                sPart.AddPart<SlideLayoutPart>(slPart);
                sPart.Slide.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone();
                SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId());           
                newSlideId.Id = slideId;           
                newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart);
                slideInsertedPostion = presentationPart.SlideParts.Count();
                presentationPart.Presentation.Save();
              
                var returnVal = ReorderSlides(presentationPart, slideInsertedPostion - 1, absolutePosition - 1);          


    internal static int ReorderSlides(PresentationPart presentationPart, int currentSlidePosition, int newPosition)       
    {           
                int returnValue = -1;

                if (newPosition == currentSlidePosition)            {                return returnValue;            }
                int slideCount = presentationPart.SlideParts.Count();
                if (slideCount == 0)            {                return returnValue;            }
                int maxPosition = slideCount - 1;
                CalculatePositions(ref currentSlidePosition, ref newPosition, maxPosition);
                if (newPosition != currentSlidePosition)           
                {               
                       DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation;               
                       SlideIdList slideIdList = presentation.SlideIdList;
                       SlideId sourceSlide = (SlideId)(slideIdList.ChildElements[currentSlidePosition]);               
                       SlideId targetSlide = (SlideId)(slideIdList.ChildElements[newPosition]);
                       sourceSlide.Remove();
                       if (newPosition > currentSlidePosition)               
                       {                   
                                 slideIdList.InsertAfter(sourceSlide, targetSlide);               
                       }               
                       else               
                       {                   
                            slideIdList.InsertBefore(sourceSlide, targetSlide);               
                       }
                       returnValue = newPosition;
                }           
                presentationPart.Presentation.Save();
          return returnValue;       

    private static void CalculatePositions(ref int originalPosition, ref int newPosition, int maxPosition)       
    {           
            if (originalPosition < 0)           
            {               
                  originalPosition = maxPosition;           
            }
            if (newPosition < 0)           
            {               
                   newPosition = maxPosition;           
            }
            if (originalPosition > maxPosition)           
            {               
                   originalPosition = maxPosition;           
            }           
            if (newPosition > maxPosition)           
            {               
                   newPosition = maxPosition;           
            }       
    }

    You can insert text into a ppt by any of these methods,
    1. Have a pre-defined content placeholder/ text placeholder and replace the Run->Text->Value of it.
    2. Create a placeholder at run-time and add it to the required slide (In this case, you need to create Run, paragraph, TextBody, Shape..).

    You can insert the text into using the first method like this,

    internal static void InsertText(this Shape shape, string content)
    {     
            DocumentFormat.OpenXml.Drawing.Text textChild = shape.TextBody.GetFirstChild<DocumentFormat.OpenXml.Drawing.Paragraph>().GetFirstChild<DocumentFormat.OpenXml.Drawing.Run>().Text;
          if ((string.IsNullOrEmpty(textChild.InnerText) || string.IsNullOrEmpty(textChild.Text)) && textChild.Text != "")           
          textChild = new DocumentFormat.OpenXml.Drawing.Text(content);      else textChild.Text = content;   
        }  
    }

    Hope this helps.. 

  • Hi pramodhegde thanks for ur reply i hope it ill work but i dont know to call the insert function i tryed 3 ways but its showing invalid argument error nu can u help me?

    protected void Button2_Click(object sender, EventArgs e)

           {

               InsertNewSlide(@"C:\Prabhu\ppp.pptx","hi amma",3);

           }

           public static void InsertNewSlide(string presentationFile, string slideTitle, int position)

           {

               // Open the source document as read/write.

               using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))

               {

                   // Pass the source document and the position and title of the slide to be inserted to the next method.

                   PresentationPart presentationPart = presentationDocument.PresentationPart;

                  InsertSlide(presentationPart, slideTitle, position);

               }

           }

           internal static void InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)

           {

    //ur code

           }

    i tryed like this and

    protected void Button1_Click(object sender, EventArgs e)

           {

               using (PresentationDocument prstDoc = PresentationDocument.Open(@"C:\Prabhu\ppp.pptx", true))

               {

                   slide = prstDoc.PresentationPart.InsertSlide("Title and Content",3);

                   Shape shape = slide.CommonSlideData.ShapeTree.Elements<Shape>().FirstOrDefault(

                       sh => sh.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value.ToLower().Equals("Content Placeholder 2".ToLower()));

                   Picture pic = slide.AddPicture(shape, @"C:\Prabhu\amma.jpg");

                   slide.CommonSlideData.ShapeTree.RemoveChild<Shape>(shape);

                   slide.Save();

                   prstDoc.PresentationPart.Presentation.Save();

               }

           }

    this also i tryed but same error

  • Hi ,

    i gave ur code like this now no error before run but will runnning its showing this error

    "Extension method must be defined in a non-generic static class"

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using DocumentFormat.OpenXml.Packaging;

    using DocumentFormat.OpenXml.Presentation;

    namespace NewOpenXml

    {

       public partial class WebForm1 : System.Web.UI.Page

       {

           protected void Page_Load(object sender, EventArgs e)

           {

           }

           protected void Button1_Click(object sender, EventArgs e)

           {

               InsertNewSlide(@"C:\Prabhu\ppp.pptx", "hi amma", 3);

           }

           public static void InsertNewSlide(string presentationFile, string slideTitle, int position)

           {

               // Open the source document as read/write.

               using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))

               {

                   // Pass the source document and the position and title of the slide to be inserted to the next method.

                   PresentationPart presentationPart = presentationDocument.PresentationPart;

                   InsertSlide(presentationPart, slideTitle, position);

               }

           }

           internal static void InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)

           {

               int slideInsertedPostion = 0;

               UInt32 slideId = 256U;

               slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count());

               Slide slide = new Slide(new CommonSlideData(new ShapeTree()));

               SlidePart sPart = presentationPart.AddNewPart<SlidePart>();

               slide.Save(sPart);

               SlideMasterPart smPart = presentationPart.SlideMasterParts.First();

               SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName));

               sPart.AddPart<SlideLayoutPart>(slPart);

               sPart.Slide.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone();

               SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId());

               newSlideId.Id = slideId;

               newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart);

               slideInsertedPostion = presentationPart.SlideParts.Count();

               presentationPart.Presentation.Save();

               var returnVal = ReorderSlides(presentationPart, slideInsertedPostion - 1, absolutePosition - 1);

           }

           internal static int ReorderSlides(PresentationPart presentationPart, int currentSlidePosition, int newPosition)

           {

               int returnValue = -1;

               if (newPosition == currentSlidePosition) { return returnValue; }

               int slideCount = presentationPart.SlideParts.Count();

               if (slideCount == 0) { return returnValue; }

               int maxPosition = slideCount - 1;

               CalculatePositions(ref currentSlidePosition, ref newPosition, maxPosition);

               if (newPosition != currentSlidePosition)

               {

                   DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation;

                   SlideIdList slideIdList = presentation.SlideIdList;

                   SlideId sourceSlide = (SlideId)(slideIdList.ChildElements[currentSlidePosition]);

                   SlideId targetSlide = (SlideId)(slideIdList.ChildElements[newPosition]);

                   sourceSlide.Remove();

                   if (newPosition > currentSlidePosition)

                   {

                       slideIdList.InsertAfter(sourceSlide, targetSlide);

                   }

                   else

                   {

                       slideIdList.InsertBefore(sourceSlide, targetSlide);

                   }

                   returnValue = newPosition;

               }

               presentationPart.Presentation.Save();

               return returnValue;

           }

           private static void CalculatePositions(ref int originalPosition, ref int newPosition, int maxPosition)

           {

               if (originalPosition < 0)

               {

                   originalPosition = maxPosition;

               }

               if (newPosition < 0)

               {

                   newPosition = maxPosition;

               }

               if (originalPosition > maxPosition)

               {

                   originalPosition = maxPosition;

               }

               if (newPosition > maxPosition)

               {

                   newPosition = maxPosition;

               }

           }

       }

    }

  • Yes. Put these methods in a class which is static (better you keep it within the same namespace).

    public static class Extensions

    {

       .....// keep all those methods here.

    }

  • OpenXMLNewSlide.zip

    Hi,

    Thanks u for ur reply ,

    In ur old code u r slide insert function is

    internal static Slide InsertSlide(this PresentationPart presentationPart, string layoutName)

    {

    //code

    }

    here u used Slide Insertslide()

    so u called like this

    Slide slide=prstDoc.PresentationPart.InsertSlide("Title and Content");

    protected void Button1_Click(object sender, EventArgs e)

           {

               using (PresentationDocument prstDoc = PresentationDocument.Open(@"C:\Prabhu\ppp.pptx", true))

               {

                   Slide slide = prstDoc.PresentationPart.InsertSlide("Title and Content");

                   Shape shape = slide.CommonSlideData.ShapeTree.Elements<Shape>().FirstOrDefault(

                       sh => sh.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value.ToLower().Equals("Content Placeholder 2".ToLower()));

                   Picture pic = slide.AddPicture(shape, @"C:\Prabhu\amma.jpg");

                   slide.CommonSlideData.ShapeTree.RemoveChild<Shape>(shape);

                   slide.Save();

                   prstDoc.PresentationPart.Presentation.Save();

               }

           }

    But now we are created in Void so How i can call ur new function

    internal static void InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)

           {

            //new code

           }

  • Modify the latest code by keeping the last line of the previous code, which actually returns the Slide.

    return GetSlide...(..);

    So, new method might look like,

    internal static Slide InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)

          {

           //new code

          return GetSlide...(..);

          }

  • Thank u my dear friend ...........thanks a lot am getting image where i need

    i hope ur Text code also work

  • Hi pramodhegde ,

    i have to create new table and have to add table datas  in new slide is it possible?

    Can u help me in this regard?

    i am waiting for ur valuable code .......Thanks in anticipation

  • Hi Prabhu,

           I have replied to your query in this thread.

  • Hello, i have tried above code. but i am getting following error.

    "IOException was unhandled. Cannot modify a read-only container."

    Can anyone help me to solve this error?

    Thanks in advance.

  • i am using code from OpenXMLNewSlide.zip . i am getting error: "Cannot implicitly convert type 'void' to 'DocumentFormat.OpenXml.Presentation.Slide'."

    Can anyone help me?

    Thanks in advance.

  • what is there to do on the tablet?

    __________

    may tinh bang

Page 1 of 1 (13 items)