예제 #1
0
        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);
        }
예제 #2
0
        /// <summary>
        /// Move a slide to a different position in the slide order in the presentation.
        /// </summary>
        /// <param name="presentationDocument"></param>
        /// <param name="from">slide index # of the source slide</param>
        /// <param name="to">slide index # of the target slide</param>
        public static void MoveSlide(PresentationDocument presentationDocument, int from, int to)
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException(StringResources.pptexceptionPowerPoint);
            }

            // Get the presentation part from the presentation document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // The slide count is not zero, so the presentation must contain slides.
            Presentation presentation = presentationPart.Presentation;
            SlideIdList  slideIdList  = presentation.SlideIdList;

            // Get the slide ID of the source slide.
            SlideId sourceSlide = slideIdList.ChildElements[from] as SlideId;
            SlideId targetSlide = slideIdList.ChildElements[to] as SlideId;

            // Remove the source slide from its current position.
            sourceSlide.Remove();

            // Insert the source slide at its new position after the target slide.
            // if the slide being moved is before the target position, use InsertAfter
            // otherwise, we want to use InsertBefore
            if (from < to)
            {
                slideIdList.InsertAfter(sourceSlide, targetSlide);
            }
            else
            {
                slideIdList.InsertBefore(sourceSlide, targetSlide);
            }

            // Save the modified presentation.
            presentation.Save();
        }
        public static int PPTReorderSlides(string fileName, int originalPosition, int newPosition)
        {
            // Assume that no slide moves; return -1.
            int returnValue = -1;

            // Moving to and from same position? Get out now.
            if (newPosition == originalPosition)
            {
                return(returnValue);
            }

            using (PresentationDocument doc = PresentationDocument.Open(fileName, true))
            {
                // Get the presentation part of the document.
                PresentationPart presentationPart = doc.PresentationPart;
                // No presentation part? Something's wrong with the document.
                if (presentationPart == null)
                {
                    throw new ArgumentException("fileName");
                }

                // If you're here, you know that presentationPart exists.
                int slideCount = presentationPart.SlideParts.Count();

                // No slides? Just return -1 indicating that nothing  happened.
                if (slideCount == 0)
                {
                    return(returnValue);
                }

                // There are slides. Calculate real positions.
                int maxPosition = slideCount - 1;

                // Adjust the positions, if necessary.
                CalcPositions(ref originalPosition, ref newPosition, maxPosition);

                // The two positions could have ended up being the same
                // thing. There's nothing to do, in that case. Otherwise,
                // do the work.
                if (newPosition != originalPosition)
                {
                    Presentation presentation = presentationPart.Presentation;
                    SlideIdList  slideIdList  = presentation.SlideIdList;

                    // Get the slide ID of the source and target slides.
                    SlideId sourceSlide =
                        (SlideId)(slideIdList.ChildElements[originalPosition]);
                    SlideId targetSlide =
                        (SlideId)(slideIdList.ChildElements[newPosition]);

                    // Remove the source slide from its parent tree. You can't
                    // move a slide while it's part of an XML node tree.
                    sourceSlide.Remove();

                    if (newPosition > originalPosition)
                    {
                        slideIdList.InsertAfter(sourceSlide, targetSlide);
                    }
                    else
                    {
                        slideIdList.InsertBefore(sourceSlide, targetSlide);
                    }

                    // Set the return value.
                    returnValue = newPosition;

                    // Save the modified presentation.
                    presentation.Save();
                }
            }
            return(returnValue);
        }