wordpress hit counter
copy a slide into another slide in powerpoint presentation - PresentationML - Formats - OpenXML Developer

copy a slide into another slide in powerpoint presentation

Formats

Discussions about working with different Open XML Formats

copy a slide into another slide in powerpoint presentation

  • rated by 0 users
  • This post has 10 Replies |
  • 0 Followers
  • hi

    I want to copy a slide with its contents into another slide in powerpoint presentation using openXml format.

     

    thanks

  • Hi,

    Do you want to achieve this programatically?

    You can copy the slide content, it's slide layout and the slide master. To create a new

    slide, create slide part, slide layout and slide master part and dump the contents copied

    into the corresponding parts.

    These links may useful to you. Some of the samples were on beta versiob of Open XML. Change

    the namespace and the relationship wherever required.


    http://openxmldeveloper.org/archive/2006/08/01/424.aspx

    http://openxmldeveloper.org/archive/2006/08/24/559.aspx

    http://openxmldeveloper.org/forums/post/1744.aspx

    Sheela

  • Thanks Sheela.

     I tried it out. Parts of the code are working correctly but these items still remain.I need to add an entry to ppt/presentation.xml. Also the slide1.xml.rels is not created automatically so how can I add it

     Your help is really apperciated

  • Hi,

    If you want to add an entry into presentation.xml file, get the content of the file in xmldocument object, add details about slidemaster and slide  as


    <p:sldMasterIdLst>
     <p:sldMasterId id="2147483648" r:id="rId1"/>
    </p:sldMasterIdLst>
    <p:sldIdLst>
     <p:sldId id="256" r:id="rId2"/>
    </p:sldIdLst>

    slide1.xml.rels file will get created when you link slide and the slide layout.

    I have a sample to create a new presentationML document. This will give you the parts and the reationship details. I am using existing xml files rather than creating a new one.

    Here is the sample code

         XmlDocument doc;
            List<string> Heading = new List<string>();//holds the contents of Slide Heading from the UI
            List<string> Body = new List<string>();//holds the contents of Slide Body from the UI
            int positionCount = 0;//holds the count of slides

            string docFileName = "";
            Uri default1 = null;

            //Declaration,Package
            Package package = null;
            PackageRelationship rel = null;
            Uri uri;
            Uri uriPart;
            PackagePart part;
            StreamWriter partWrt;
            PackagePart getPart;

            int slId = 256;//Id given to the slide 1 in the ppt,which goes on incrementing by 1 for every new slide added
            int relationId = 2;

            int resourceId = 0;
            //Declaration,Namespace
            string nameSpace = "http://schemas.openxmlformats.org/presentationml/2006/main";
            string nameSpaceDrawing = "http://schemas.openxmlformats.org/drawingml/2006/main";
            string nameSpaceRel = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
            string fileName;

            public createPPTx()
            {
                InitializeComponent();
            }

            private void createPPTx_Load(object sender, EventArgs e)
            {
                fileName = GetSavePath();
                createPresentation();
                createSlide(1);
                addresource();

               
            }

            private void addresource()
            {
                uri = new Uri("/ppt/slideLayout1.xml", UriKind.Relative);
                part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml");

                partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite));
                doc = new XmlDocument();

                doc.Load(@"D:\adonet2\systemIO\systemIO\slideLayout1.xml");
                doc.Save(partWrt);
                partWrt.Close();

                package.Flush();

                uri = new Uri("/ppt/slideMaster1.xml", UriKind.Relative);
                part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml");

                partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite));
                doc = new XmlDocument();
                doc.Load(@"D:\adonet2\systemIO\systemIO\slideMaster1.xml");
                doc.Save(partWrt);
                partWrt.Close();

                package.Flush();

                //create relationship to the slidemaster
                uri = new Uri("slideMaster1.xml", UriKind.Relative);
                uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative);
                getPart = package.GetPart(uriPart);

                rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", "rId2");//

                package.Flush();
                //create relationship to the slide layout

                uri = new Uri("slideLayout1.xml", UriKind.Relative);
                uriPart = new Uri("/ppt/slideMaster1.xml", UriKind.Relative);
                getPart = package.GetPart(uriPart);

                rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId1");//

                package.Flush();

                // create a relationship from slide to slidelayout
                uri = new Uri("slideLayout1.xml", UriKind.Relative);
                uriPart = new Uri("/ppt/slide1.xml", UriKind.Relative);
                getPart = package.GetPart(uriPart);

                rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId1");//

                package.Flush();

                //create relationship from layout to master
                uri = new Uri("slideMaster1.xml", UriKind.Relative);
                uriPart = new Uri("/ppt/slideLayout1.xml", UriKind.Relative);
                getPart = package.GetPart(uriPart);

                rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", "rId2");//

                package.Flush();

            }

     public void createPresentation()
            {
                package = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite);

                XmlDocument docPresentation = new XmlDocument();
              
                docPresentation.Load(@"D:\adonet2\systemIO\systemIO\presentation.xml");

                default1 = new Uri("/default.xml", UriKind.Relative);
                PackagePart part = package.CreatePart(default1, "application/xml");

                uri = new Uri("/ppt/presentation.xml", UriKind.Relative);
                part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml");
                partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));
                docPresentation.Save(partWrt);
                partWrt.Close();

                package.Flush();


                // Create the relationship file
                uri = new Uri("/ppt/presentation.xml", UriKind.Relative);
                rel = package.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");

                package.Flush();

              
            }

      public void createSlide(int slideCount)
            {
              
               // string FileName = "slide1";
             
                string rIdStr = "rId3";
                XmlDocument doc = new XmlDocument();
                doc.Load(@"D:\adonet2\systemIO\systemIO\slide1.xml");


                uri = new Uri("/ppt/slide1.xml", UriKind.RelativeOrAbsolute);
                part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml");
                partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));
                doc.Save(partWrt);

                partWrt.Close();
                package.Flush();

                //Create the relationship file

                uri = new Uri("slide1.xml", UriKind.Relative);
                uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative);
                getPart = package.GetPart(uriPart);
                // PackageRelationship
                rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", rIdStr);//

                package.DeletePart(default1);
                package.Flush();

     

            }


      private string GetSavePath()
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.AddExtension = true;
                //Get only Docx file
                sfd.Filter = "pptx|";
                sfd.CheckPathExists = true;
                sfd.DefaultExt = ".pptx";

                sfd.ShowDialog();

                return sfd.FileName;
                // return the filename and the path in which the user wants to create the file
            }

     

    Sheela

  • hi sheela;

    Thanks for your reply, but ia have a problem while copying the slide, The new slide is empty.

     Also the pptx file gave me an error while opening about corrupting in some of the slide components like "Text and Image... etc" .

    Here is the code that i wrote

    //Copying slide (1) into new slide (6):

    XmlDocument doc = new XmlDocument();

    doc.Load("c:\\slide1.xml");

    uri = new Uri("/ppt/slides/slide6.xml", UriKind.RelativeOrAbsolute);

    part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml");

    StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write));

    doc.Save(partWrt);

    partWrt.Close();

    package.Flush();

    //Create the relationship file

    uri = new Uri("/ppt/slides/slide6.xml", UriKind.Relative);

    Uri uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative);

    PackagePart getPart = package.GetPart(uriPart);

    // PackageRelationship

    PackageRelationship rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", "rId60");//

    package.Flush();

     

    ////3. Add slide master and slide layout as parts and set the relationship (slide inherits layout details from slidelayout and slidemaster)

    uri = new Uri("/ppt/slideLayout6.xml", UriKind.Relative);

    part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml");

    partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite));

    doc = new XmlDocument();

    doc.Load("c:\\slideLayout1.xml");

    doc.Save(partWrt);

    partWrt.Close();

    package.Flush();

     

    uri = new Uri("/ppt/slideMaster6.xml", UriKind.Relative);

    part = package.CreatePart(uri, "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml");

    partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.ReadWrite));

    doc = new XmlDocument();

    doc.Load("c:\\slideMaster1.xml");

    doc.Save(partWrt);

    partWrt.Close();

    package.Flush();

    //create relationship to the slidemaster

    uri = new Uri("ppt/slideMaster6.xml", UriKind.Relative);

    uriPart = new Uri("/ppt/presentation.xml", UriKind.Relative);

    getPart = package.GetPart(uriPart);

    rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", "rId61");//

    package.Flush();

    //create relationship to the slide layout

    uri = new Uri("/ppt/slideLayout6.xml", UriKind.Relative);

    uriPart = new Uri("/ppt/slideMaster6.xml", UriKind.Relative);

    getPart = package.GetPart(uriPart);

    rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId62");//

    package.Flush();

    //connect slide layout with the slide

    // create a relationship from slide to slidelayout

    uri = new Uri("/ppt/slideLayout6.xml", UriKind.Relative);

    uriPart = new Uri("/ppt/slides/slide6.xml", UriKind.Relative);

    getPart = package.GetPart(uriPart);

    rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId63");//

    package.Flush();

    //create relationship from layout to master

    uri = new Uri("/ppt/slideMaster6.xml", UriKind.Relative);

    uriPart = new Uri("/ppt/slideLayout6.xml", UriKind.Relative);

    getPart = package.GetPart(uriPart);

    rel = getPart.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", "rId64");//

    package.Flush();

    //modify the presentation.xml

    XmlDocument xDoc = new XmlDocument(nt);

    xDoc.Load(documentPart.GetStream());

    XmlNodeList sheetNodes = xDoc.SelectNodes("//p:sldIdLst/p:sldId", nsManager);

    XmlElement x = (XmlElement)sheetNodes[0].CloneNode(true);

    x.SetAttribute("id", "257");

    x.SetAttribute("r:id", "rId3");

    XmlNode parentNode = sheetNodes[0].ParentNode;

    parentNode.AppendChild(x);

    xDoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));

  • Hi,

    I am giving a sample code for copying a slide into another.Please try doing this.       

         string nameSpace = "http://schemas.openxmlformats.org/presentationml/2006/main";
                string presentationRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
                const string sliderelationship = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide";
                Uri presentaionUri = null;
                Uri slideUri = null;
                Uri layoutUri = null;
                PackagePart presentationPart = null;
                PackagePart slidePart = null;
                PackagePart oldslidePart = null;
    //opening the package
                Package pack = Package.Open(path,FileMode.Open, FileAccess.ReadWrite);
               
                foreach (System.IO.Packaging.PackageRelationship relationship in pack.GetRelationshipsByType(presentationRelationshipType))
                {
                  presentaionUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                  presentationPart = pack.GetPart(presentaionUri);
                    break;
                }
                NameTable nt = new NameTable();
                XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
                nsManager.AddNamespace("p", nameSpace);
                XmlDocument xdoc = new XmlDocument(nt);

    //loading the presentation.xml and making entry for the new slide
                xdoc.Load(presentationPart.GetStream());
                MessageBox.Show(xdoc.InnerXml.ToString());
                XmlNode slidelistNode = xdoc.SelectSingleNode("//" + "p:sldIdLst", nsManager);
                XmlNode slideidNode = xdoc.CreateNode(XmlNodeType.Element, "p:sldId", nameSpace);
                XmlNode sld= slidelistNode.FirstChild;
                XmlNode dplicate= sld.Clone();
                XmlAttributeCollection atts = dplicate.Attributes;
             foreach (XmlAttribute att in atts)
             {
                 if (att.Name == "id")
                 {
                     att.Value = "257";
                 }
                 if (att.Name == "r:id")
                 {
                     att.Value = "rId10";
                 }
             }
             slidelistNode.AppendChild(dplicate);
             xdoc.Save(presentationPart.GetStream());

    //loading the slide part which is to be copied
            
             foreach (System.IO.Packaging.PackageRelationship rels in presentationPart.GetRelationshipsByType(sliderelationship))
                {
                  Uri sUri= PackUriHelper.ResolvePartUri(presentaionUri, rels.TargetUri);
                  oldslidePart = pack.GetPart(sUri);
                    break;
                }
                XmlDocument oldslideDoc = new XmlDocument(nt);
                oldslideDoc.Load(oldslidePart.GetStream());

    //making the new slide part and copying the content in it.
              
                slideUri = new Uri("/ppt/slides/slide3.xml", UriKind.RelativeOrAbsolute);
                slidePart = pack.CreatePart(slideUri, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml");
                StreamWriter slideWriter = null;
                slideWriter = new StreamWriter(slidePart.GetStream(FileMode.Create, FileAccess.Write));
                oldslideDoc.Save(slideWriter);
                oldslideDoc.Load(slidePart.GetStream());
                MessageBox.Show(oldslideDoc.InnerXml.ToString());
                slideWriter.Close();
                pack.Flush();

    //creating the new slide's relationship with slide layout
               
                layoutUri = new Uri("/ppt/slideLayouts/slideLayout1.xml", UriKind.Relative);
                PackageRelationship slideRels = null;
                slideRels = slidePart.CreateRelationship(layoutUri, TargetMode.Internal,

    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout", "rId10");
                pack.Flush();

    //creating the new slide's relationship with presentation part.

                PackageRelationship pptsliderels = null;
                pptsliderels = presentationPart.CreateRelationship(slideUri, TargetMode.Internal,                                   

    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", "rId10");
                pack.Flush();


               

    Mallika

  • Hi1

    How can I copy a Slide into another powerpoint presentation with use SDK like this?

     

    Dim pptCre As PresentationDocument = PresentationDocument.Open("C:\Documents and Settings\CHT\My Documents\pptx\Test1.pptx", PresentationDocumentType.Presentation)

    Dim pptCre2 As PresentationDocument = PresentationDocument.Open("C:\Documents and Settings\CHT\My Documents\pptx\Test1.pptx", PresentationDocumentType.Presentation)

    Dim slide As PresentationPart = pptCre2.part("/ppt/slides/slide5.xml")

    pptCre.AddPart(slide)

    pptCre.Close()

  • Hi,

    As your question is slightly different from the discussion that was going on in this thread,please put your
    question as a new question.

    Mallika

  • Does any java api has this capability.
  • Hi,

    I guess Java Api doesn't do that.
    But you can go through the documentation of openxml4j API.

    Mallika

  • Hi

    i have started working on openxml. i am struck at inserting table in presentation slide.

    Can you pls send me code snippet if have.

    And also i am facing problem in inserting image into presentation.

    Regards
    sanjay
Page 1 of 1 (11 items)