title: Copying a slide from one presentation to another using Open XML SDK
body: <p><em>Sorry my English :)</em></p>
  
  <p>There is a code</p>
  
  <pre><code>using (var sourceDoc = PresentationDocument.Open(@"d:\source.pptx", false))
  {
      using (var destDoc = PresentationDocument.Open(@"d:\dest.pptx", true))
      {
  
      }
  }
  </code></pre>
  
  <p><strong>I try copy slide №2 from <code>sourceDoc</code> and paste into <code>destDoc</code> position 4.</strong>                        </p>
  
  <hr>
  
  <p>There are articles with same title <a href="http://geekswithblogs.net/tmurphy/archive/2010/03/20/copying-a-slide-from-one-presentation-to-another.aspx" rel="nofollow noreferrer">"Copying A Slide From One Presentation To Another"</a> and <a href="https://blogs.msdn.microsoft.com/brian_jones/2009/03/05/how-to-assemble-multiple-powerpoint-decks/" rel="nofollow noreferrer">"How to Assemble Multiple PowerPoint Decks"</a>, but unfortunately I cannot apply that. I just confused. For example: </p>
  
  <pre><code>uniqueId = GetMaxIdFromChild(destPresPart.Presentation.SlideMasterIdList);
  </code></pre>
  
  <p>what does mean? Compile time say error.</p>
  
  <hr>
  
  <h2>UPDATE #1</h2>
  
  <p>I created method to copy slide to another presentation</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Copy one slide to another presentation
  /// &lt;/summary&gt;
  /// &lt;param name="sourcePresentationPath"&gt;&lt;/param&gt;
  /// &lt;param name="slidePosition"&gt;
  /// Slide number from source presentation which will be copy to destinition presentation
  /// &lt;/param&gt;
  /// &lt;param name="destPresentationPath"&gt;&lt;/param&gt;
  /// &lt;remarks&gt;Slide copy to end destinition presentation&lt;/remarks&gt;
  public static void CopySlideTo(string sourcePresentationPath, int slidePosition, string destPresentationPath)
  {
      using (PresentationDocument sourcePresentationDocument = PresentationDocument.Open(sourcePresentationPath, false))
      {
          var sourcePresentationPart = sourcePresentationDocument.PresentationPart;
          var sourcePresentation = sourcePresentationPart.Presentation;
          SlideIdList sourceSlideIdList = sourcePresentation.SlideIdList;
          SlideId slideIdSelectedSlide = sourceSlideIdList.ChildElements[slidePosition - 1] as SlideId;
          SlidePart sourceSlidePart = sourcePresentationPart.GetPartById(slideIdSelectedSlide.RelationshipId) as SlidePart;
  
          SlidePart destSlidePart = null;
          SlideIdList destSlideIdList = null;
          PresentationPart destPresentationPart = null;
          using (PresentationDocument destPresentationDocument = PresentationDocument.Open(destPresentationPath, true))
          {
              var addedSlidePart = destPresentationDocument.PresentationPart.AddPart(sourceSlidePart);                    
  
              destSlideIdList = destPresentationDocument.PresentationPart.Presentation.SlideIdList;
  
              destPresentationPart = destPresentationDocument.PresentationPart;                   
  
              SlideId lastSlideIdInDestPresentation = destSlideIdList.ChildElements.Last() as SlideId;
  
              // Insert the new slide into the slide list after last slide 
              SlideId addedSlideId = destSlideIdList.InsertAfter(new SlideId(), lastSlideIdInDestPresentation);
              addedSlideId.Id = lastSlideIdInDestPresentation.Id++;
              addedSlideId.RelationshipId = destPresentationPart.GetIdOfPart(addedSlidePart);
  
              destPresentationPart.Presentation.Save();
          }
      }
  </code></pre>
  
  <p>Slide is copied, but without background. How also move background? </p>
  
  <hr>
  
  <h2>UPDATE #2</h2>
  
  <p>I finally created method to copy a slide to another presentation</p>
  
  <pre><code>public class Extensions
  {
      static uint uniqueId;                       
  
      /// &lt;summary&gt;
      /// Copy one slide to another presentation
      /// &lt;/summary&gt;
      /// &lt;param name="sourcePresentationPath"&gt;&lt;/param&gt;
      /// &lt;param name="slidePosition"&gt;
      /// Slide number from source presentation which will be copy to destinition presentation
      /// &lt;/param&gt;
      /// &lt;param name="destPresentationPath"&gt;&lt;/param&gt;
      /// &lt;remarks&gt;Slide is copied to end destinition presentation&lt;/remarks&gt;
      [SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
      public static void CopySlideTo(string sourcePresentationPath, int slidePosition, string destPresentationPath)
      {
          using (PresentationDocument sourcePresentationDocument = PresentationDocument.Open(sourcePresentationPath, false))
          {
              var sourcePresentationPart = sourcePresentationDocument.PresentationPart;
              var sourcePresentation = sourcePresentationPart.Presentation;
              SlideIdList sourceSlideIdList = sourcePresentation.SlideIdList;
              SlideId slideIdSelectedSlide = sourceSlideIdList.ChildElements[slidePosition - 1] as SlideId;
              SlidePart sourceSlidePart = sourcePresentationPart.GetPartById(slideIdSelectedSlide.RelationshipId) as SlidePart;
              using (PresentationDocument destPresentationDocument = PresentationDocument.Open(destPresentationPath, true))
              {
                  var addedSlidePart = destPresentationDocument.PresentationPart.AddPart(sourceSlidePart);                    
  
                  var destSlideIdList = destPresentationDocument.PresentationPart.Presentation.SlideIdList;
  
                  var destPresentationPart = destPresentationDocument.PresentationPart;                   
  
                  SlideId lastSlideIdInDestPresentation = destSlideIdList.ChildElements.Last() as SlideId;
  
                  // Insert the new slide into the slide list after last slide 
                  SlideId addedSlideId = destSlideIdList.InsertAfter(new SlideId(), lastSlideIdInDestPresentation);
                  addedSlideId.Id = lastSlideIdInDestPresentation.Id++;
                  addedSlideId.RelationshipId = destPresentationPart.GetIdOfPart(addedSlidePart);
  
                  // Get the existing slide master part.
                  SlideMasterPart destPresPartSlideMasterPart = destPresentationPart.SlideMasterParts.ElementAt(0);
                  string relationshipId = destPresentationPart.GetIdOfPart(destPresPartSlideMasterPart);
  
                  // Get the new slide master part.
                  SlideMasterPart sourcePresPartSlideMasterPart = sourcePresentationPart.SlideMasterParts.ElementAt(0);
  
                  // Remove the existing theme part.
                  destPresentationPart.DeletePart(destPresentationPart.ThemePart);
  
                  // Remove the old slide master part.
                  destPresentationPart.DeletePart(destPresPartSlideMasterPart);
  
                  // Import the new slide master part, and reuse the old relationship ID.
                  sourcePresPartSlideMasterPart = destPresentationPart.AddPart(sourcePresPartSlideMasterPart, relationshipId);
  
                  // Change to the new theme part.
                  destPresentationPart.AddPart(sourcePresPartSlideMasterPart.ThemePart);
  
                  var newSlideLayouts = new Dictionary&lt;string, SlideLayoutPart&gt;();
  
                  foreach (var slideLayoutPart in sourcePresPartSlideMasterPart.SlideLayoutParts)
                  {
                      newSlideLayouts.Add(GetSlideLayoutType(slideLayoutPart), slideLayoutPart);
                  }
  
                  // Remove the slide layout relationship on all slides. 
                  foreach (var slidePart in destPresentationPart.SlideParts)
                  {                        
                      string layoutType = null;
  
                      if (slidePart.SlideLayoutPart != null)
                      {
                          // Determine the slide layout type for each slide.
                          layoutType = GetSlideLayoutType(slidePart.SlideLayoutPart);
  
                          // Delete the old layout part.
                          slidePart.DeletePart(slidePart.SlideLayoutPart);
                      }
  
                      SlideLayoutPart newLayoutPart = null;
                      if (layoutType != null &amp;&amp; newSlideLayouts.TryGetValue(layoutType, out newLayoutPart))
                      {
                          // Apply the new layout part.
                          slidePart.AddPart(newLayoutPart);
                      }
                  }
                  FixSlideLayoutIds(destPresentationPart);
                  destPresentationPart.Presentation.Save();                    
              }
          }
      }
  
      static void FixSlideLayoutIds(PresentationPart presPart)
      {
          // Make sure that 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();
          }
      }
  
      /// &lt;summary&gt;
      /// Get the slide layout type.
      /// &lt;/summary&gt;
      /// &lt;param name="slideLayoutPart"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private static string GetSlideLayoutType(SlideLayoutPart slideLayoutPart)
      {
          CommonSlideData slideData = slideLayoutPart.SlideLayout.CommonSlideData;
  
          return slideData.Name;
      }
  }
  
  // Client code
  Extensions.CopySlideTo(@"D:\temp\source.pptx", 1, @"D:\temp\dest.pptx");
  </code></pre>
  
  <p>Now when I open result (<code>d:\temp\dest.pptx</code>) see</p>
  
  <p><a href="https://i.stack.imgur.com/JhERI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JhERI.jpg" alt="enter image description here"></a></p>
  
  <p>However, if click <em>Repair</em> result is ok.</p>
  
  <p><strong>Why this is happend? How fix it?</strong> I suspect that something wrong with slide layout adding.</p>