Sorry my English :)
There is a code
using (var sourceDoc = PresentationDocument.Open(@"d:\source.pptx", false))
{
using (var destDoc = PresentationDocument.Open(@"d:\dest.pptx", true))
{
}
}
I try copy slide №2 from sourceDoc
and paste into destDoc
position 4.
There are articles with same title "Copying A Slide From One Presentation To Another" and "How to Assemble Multiple PowerPoint Decks", but unfortunately I cannot apply that. I just confused. For example:
uniqueId = GetMaxIdFromChild(destPresPart.Presentation.SlideMasterIdList);
what does mean? Compile time say error.
UPDATE #1
I created method to copy slide to another presentation
/// <summary>
/// Copy one slide to another presentation
/// </summary>
/// <param name="sourcePresentationPath"></param>
/// <param name="slidePosition">
/// Slide number from source presentation which will be copy to destinition presentation
/// </param>
/// <param name="destPresentationPath"></param>
/// <remarks>Slide copy to end destinition presentation</remarks>
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();
}
}
Slide is copied, but without background. How also move background?
UPDATE #2
I finally created method to copy a slide to another presentation
public class Extensions
{
static uint uniqueId;
/// <summary>
/// Copy one slide to another presentation
/// </summary>
/// <param name="sourcePresentationPath"></param>
/// <param name="slidePosition">
/// Slide number from source presentation which will be copy to destinition presentation
/// </param>
/// <param name="destPresentationPath"></param>
/// <remarks>Slide is copied to end destinition presentation</remarks>
[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<string, SlideLayoutPart>();
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 && 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();
}
}
/// <summary>
/// Get the slide layout type.
/// </summary>
/// <param name="slideLayoutPart"></param>
/// <returns></returns>
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");
Now when I open result (d:\temp\dest.pptx
) see
However, if click Repair result is ok.
Why this is happend? How fix it? I suspect that something wrong with slide layout adding.
uniqueId
is not variable and not some C# keyword. – BoxthornOpen XML SDK Productivity Tool
. Yes, that tool shows how look C# code for certain .pptx, but I need to know proccess copy slide to another presentation. – Boxthorn