none
Copying slides between presentations and applying destination presentations master part RRS feed

  • Question

  • We are copying slides from one presentation to another using logic given at http://msdn.microsoft.com/en-us/library/office/ee361883(v=office.12).aspx. This adds a new slide master part for every slide from the source. Instead we want to be able to add slides and inherit the slide master of the destination presentation just as it happens when we copy and paste the slide manually. We should also be add the slide layout part of source slide if does not already exist in the destination slide master. Any pointers would be really helpful.
    Thursday, October 4, 2012 5:39 PM

Answers

  • Hi Keerthiga,

    Would you please try to add a new sildelayout part and use FeedData to copy the slidelayout part to the destination presentation?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 10, 2012 7:30 AM
    Moderator

All replies

  • Hi Keerthiga,

    Thanks for posting in the MSDN Forum.

    I’m not sure about your scenario of “We should also be add the slide layout part of source slide if does not already exist in the destination slide master.” As far as I know, destination presentation will not inherit any slidemaster from the source present if we copy the slide from the source presentation to destination presentation via Ctrl+C and Ctrl+V. You description let me confused with your goal. Would you please show me more detailed scenario?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 5, 2012 2:35 AM
    Moderator
  • Hi Tom,

    Thanks for the prompt reply. We are not copying the slide manually via Ctrl+C and Ctrl+V. We use the following code,

     static void MergeSlides(string presentationFolder,
        string sourcePresentation, string destPresentation)
      {
        int id = 0;

        // Open the destination presentation.
        using (PresentationDocument myDestDeck =
          PresentationDocument.Open(presentationFolder + destPresentation,
          true))
        {
          PresentationPart destPresPart = myDestDeck.PresentationPart;

          // If the merged presentation does not have a SlideIdList
          // element yet, add it.
          if (destPresPart.Presentation.SlideIdList == null)
            destPresPart.Presentation.SlideIdList = new SlideIdList();

          // Open the source presentation. This will throw an exception if
          // the source presentation does not exist.
          using (PresentationDocument mySourceDeck =
            PresentationDocument.Open(
              presentationFolder + sourcePresentation, false))
          {
            PresentationPart sourcePresPart =
              mySourceDeck.PresentationPart;

            // Get unique ids for the slide master and slide lists
            // for use later.
            uniqueId =
              GetMaxSlideMasterId(
                destPresPart.Presentation.SlideMasterIdList);

            uint maxSlideId =
              GetMaxSlideId(destPresPart.Presentation.SlideIdList);

            // Copy each slide in the source presentation, in order, to
            // the destination presentation.
            foreach (SlideId slideId in
              sourcePresPart.Presentation.SlideIdList)
            {
              SlidePart sp;
              SlidePart destSp;
              SlideMasterPart destMasterPart;
              string relId;
              SlideMasterId newSlideMasterId;
              SlideId newSlideId;

              // Create a unique relationship id.
              id++;
              sp =
                (SlidePart)sourcePresPart.GetPartById(
                  slideId.RelationshipId);

              relId =
                sourcePresentation.Remove(
                  sourcePresentation.IndexOf('.')) + id;

              // Add the slide part to the destination presentation.
              destSp = destPresPart.AddPart<SlidePart>(sp, relId);

              // The slide master part was added. Make sure the
              // relationship between the main presentation part and
              // the slide master part is in place.
              destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
              destPresPart.AddPart(destMasterPart);

              // Add the slide master id to the slide master id list.
              uniqueId++;
              newSlideMasterId = new SlideMasterId();
              newSlideMasterId.RelationshipId =
                destPresPart.GetIdOfPart(destMasterPart);
              newSlideMasterId.Id = uniqueId;

              destPresPart.Presentation.SlideMasterIdList.Append(
                newSlideMasterId);

              // Add the slide id to the slide id list.
              maxSlideId++;
              newSlideId = new SlideId();
              newSlideId.RelationshipId = relId;
              newSlideId.Id = maxSlideId;

              destPresPart.Presentation.SlideIdList.Append(newSlideId);
            }

            // Make sure that all slide layout ids are unique.
            FixSlideLayoutIds(destPresPart);
          }

          // Save the changes to the destination deck.
          destPresPart.Presentation.Save();
        }
      }

    Above you can see that a new slide master is added. Instead we want to apply the destination presentation's slide master to the newly copied slide and the layout of the slide should be added to the slide master if does not already exist. Please let me know if you need further information.

    Friday, October 5, 2012 5:41 AM
  • Hi Keerthiga,

    Would you please try to add a new sildelayout part and use FeedData to copy the slidelayout part to the destination presentation?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 10, 2012 7:30 AM
    Moderator
  • Hi Tom

    I'm trying to apply this to a project of my own, but I'm yet not that keen on Open XML.

    Can you specify how Keerthigas code should be changed to apply the FeedData-approach?

    Thanks in advance


    Best Regards Peter Karlström Midrange AB, Sweden

    Friday, January 25, 2013 7:16 AM
  • Hi.

    Some years later, I have the same problem.

    Did anybody solve the problem? Did the FeedData-Approach work?

    What I want to do:

    Check, if the slidemaster and the slidelayout which is used in the new(copied) slide, is already in the merged presentation. If that is the case, I want the slide to use(reference) the already existing layout and master instead of copying a new identical slidemaster with all it's layouts again to the merged presentation. If you copy more than one slide you get a separate identical master/layout entry for each of the slides.

    For my solution it would be sufficient if I were able to compare the slidemasters' names. The layouts' name are no problem. They are properties of the commonSlideData-Object and can be found in SlidePart.SlideLayoutPart.SlideLayout.CommonSlideData.Name.

    The problem are the slidemasters' names: In analogy I would expect to find them in SlidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.CommonSlideData.Name or the other way round in PresentationDocument.PresentationPart.SlideMasterPart.SlideMaster(0).CommonSlideData.Name.

    The slidemasters' names are listed in the TitlesOfParts-Vector which you can find in PresentationDocument.ExtendedFilePropertiesPart.Properties.TitlesOfParts.VTVector.ChildElements. The Vector contains some StringValues and amongst others the used slidemasters' names are listed, which you can see in Powerpoint if you put your mouse over the master slide when slide master view is activated. The problem here is the missing connection between the slidemaster and it's name (title).

    Does anyone know how a connection between the masters' names in TitlesOfParts and the slidemasters can be established?

    Or does anyone know another way to connect a slidemaster to it's name?

    Thank you for help and sorry for reactivating an ancient thread ...

    Karl

    Wednesday, June 7, 2017 3:05 PM
  • Sorry to reactivate this ancient thread again, but curious if anybody found a solution to this.  We are trying to do something similar where we copy a slide to a new presentation and we don't want to duplicate any masters or layouts.  We would like to apply the same layout to the slide in the destination file with the assumption that the layout names are consistent between the files.
    Tuesday, November 20, 2018 11:19 PM