private int ReorderSlides(string fileName, int currentSlidePosition, int newPosition) { int returnValue = -1; if (newPosition == currentSlidePosition) { return(returnValue); } using (PresentationDocument doc = PresentationDocument.Open(fileName, true)) { PresentationPart presentationPart = doc.PresentationPart; if (presentationPart == null) { throw new ArgumentException("Presentation part not found."); } int slideCount = presentationPart.SlideParts.Count(); if (slideCount == 0) { return(returnValue); } int maxPosition = slideCount - 1; CalculatePositions(ref currentSlidePosition, ref newPosition, maxPosition); if (newPosition != currentSlidePosition) { DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; SlideIdList slideIdList = presentation.SlideIdList; SlideId sourceSlide = (SlideId)(slideIdList.ChildElements[currentSlidePosition]); SlideId targetSlide = (SlideId)(slideIdList.ChildElements[newPosition]); sourceSlide.Remove(); if (newPosition > currentSlidePosition) { slideIdList.InsertAfter(sourceSlide, targetSlide); } else { slideIdList.InsertBefore(sourceSlide, targetSlide); } returnValue = newPosition; presentation.Save(); } } return(returnValue); }
public static void DeleteSlide(string presentationFile) { if (presentationFile == null) { throw new ArgumentNullException("presentationDocument"); } using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true)) { // Get the presentation part from the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the presentation from the presentation part. DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; int slideIdx = -1; foreach (SlideId _slideId in presentation.SlideIdList) { slideIdx++; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIdx] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // Get the slide part for the specified slide. SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; if (slidePart.Slide.Show != null) { if (slidePart.Slide.Show.HasValue != null) { // Remove the slide from the slide list. slideIdList.RemoveChild(slideId); // Remove references to the slide from all custom shows. if (presentation.CustomShowList != null) { // Iterate through the list of custom shows. foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { // Declare a link list of slide list entries. LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // Find the slide reference to remove from the custom show. if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // Remove all references to the slide from the custom show. foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } // Save the modified presentation. presentation.Save(); // Remove the slide part. presentationPart.DeletePart(slidePart); break; } } } } }