wordpress hit counter
Add selected slides to an empty presentation. - Open XML SDK 2.0 - Development Tools - OpenXML Developer

Add selected slides to an empty presentation.

Development Tools

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

Add selected slides to an empty presentation.

  • rated by 0 users
  • This post has 2 Replies |
  • 2 Followers
  • Hi!

    Given list of slide numbers i want to copy those slides into an empty presentation document.

    I started from this blogpost: http://blogs.msdn.com/b/brian_jones/archive/2009/03/05/how-to-assemble-multiple-powerpoint-decks.aspx

    In this you give 2 presentation documents and the algo copies the first one into the second. With a few modifications i've managed to copy only the slide i want from the first presentation into the second, and it works well. but if the second presentation document is an empty document the code still runs without an error, but powerpoint says the document needs to be repaired, and i don't know why. Can you help me? Here are the modifications i made:

    a) at GetMaxIdFromChild(OpenXmlElement el) i added the part: if el == null

    b) around the end of mergedecks i added the part: if (destPresPart.Presentation.SlideIdList == null)



        class Program
        {
            //We will use this id for both the slide master id and slide layout id lists
            static uint uniqueId;
            static int reqid = 1;
            static void Main(string[] args)
            {
                string destDeck = "output.pptx";
                //var first = "grey.pptx";
                var first = "cloneable.pptx";
                var second = "tocloneinto.pptx";
    
                File.Copy(first, destDeck, true);
                MergeDecks(second, destDeck);
            }
    
            static void MergeDecks(string sourceDeck, string destDeck)
            {
                int id = 1;
                //Open up the destination deck
                using (PresentationDocument myDestDeck = PresentationDocument.Open(destDeck, true))
                {
                    PresentationPart destPresPart = myDestDeck.PresentationPart;
    
                    //Open up the source deck
                    using (PresentationDocument mySourceDeck = PresentationDocument.Open(sourceDeck, true))
                    {
                        PresentationPart sourcePresPart = mySourceDeck.PresentationPart;
    
                        //Need to get a unique ids for slide master and slide lists (will use this later)
                        uniqueId = GetMaxIdFromChild(destPresPart.Presentation.SlideMasterIdList);
                        uint maxSlideId = GetMaxIdFromChild(destPresPart.Presentation.SlideIdList);
    
                        //Copy each slide in my source deck in order to my destination deck
                        foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
                        {
                            int actSlideNumber = mySourceDeck.PresentationPart.Presentation.SlideIdList.ToList().IndexOf(slideId) + 1;
                            if (actSlideNumber != reqid) continue;
    
                            SlidePart sp;
                            SlidePart destSp;
                            SlideMasterPart destMasterPart;
                            string relId;
                            SlideMasterId newSlideMasterId;
                            SlideId newSlideId;
    
                            //come up with a unique relationship id
                            id++;
                            sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);
                            relId = sourceDeck.Remove(sourceDeck.IndexOf('.')) + id;
                            destSp = destPresPart.AddPart<SlidePart>(sp, relId);
    
                            //Master part was added, but now we need to make sure the relationship is in place
                            destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
                            destPresPart.AddPart(destMasterPart);
    
                            //Add slide master to slide master list
                            uniqueId++;
                            newSlideMasterId = new SlideMasterId();
                            newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart);
                            System.Diagnostics.Trace.WriteLine("newSlideMasterId.RelationshipId: " + newSlideMasterId.RelationshipId);
                            newSlideMasterId.Id = uniqueId;
    
                            //Add slide to slide list
                            maxSlideId++;
                            newSlideId = new SlideId();
                            newSlideId.RelationshipId = relId;
                            newSlideId.Id = maxSlideId;
    
                            if (destPresPart.Presentation.SlideIdList == null)
                            {                         
                                destPresPart.Presentation.SlideIdList = new SlideIdList();
                            }
                            destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId);
                            destPresPart.Presentation.SlideIdList.Append(newSlideId);
                        }
                        //Make sure all slide ids are unique
                        FixSlideLayoutIds(destPresPart);
                    }
                    destPresPart.Presentation.Save();
                }
            }
    
            static void FixSlideLayoutIds(PresentationPart presPart)
            {
                //Need to make sure all slide layouts have unique ids
                foreach (SlideMasterPart slideMasterPart in presPart.SlideMasterParts)
                {
                    foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
                    {
                        uniqueId++;
                        slideLayoutId.Id = (uint)uniqueId;
                    }
                    slideMasterPart.SlideMaster.Save();
                }
            }
    
            static uint GetMaxIdFromChild(OpenXmlElement el)
            {
                uint max = 1;
                //Get max id value from set of children
    
                if (el == null)
                {
                    return 1;
                }
                foreach (OpenXmlElement child in el.ChildElements)
                {
                    OpenXmlAttribute attribute = child.GetAttribute("id", "");
    
                    uint id = uint.Parse(attribute.Value);
    
                    if (id > max)
                        max = id;
                }
                return max;
            }
        }
    




  • Hi,

    One more best link for you to refer is;

    openxmldeveloper.org/.../110420.aspx

  • oooohh! perfect! thank you very much!

Page 1 of 1 (3 items)