private SlidePart CloneSlidePart(PresentationPart presentationPart, SlidePart slideTemplate) { SlidePart cloneSlidei = presentationPart.AddNewPart <SlidePart>("newSlide" + i); i++; cloneSlidei.FeedData(slideTemplate.GetStream(FileMode.Open)); cloneSlidei.AddPart(slideTemplate.SlideLayoutPart); SlideIdList slideidlist = presentationPart.Presentation.SlideIdList; uint maxide = 0; SlideId beforeSlide = null; foreach (SlideId slideidw in slideidlist.ChildElements) { if (slideidw.Id > maxide) { beforeSlide = slideidw; maxide = slideidw.Id; } } maxide++; SlideId inside = slideidlist.InsertAfter(new SlideId(), beforeSlide); inside.Id = maxide; inside.RelationshipId = presentationPart.GetIdOfPart(cloneSlidei); return(cloneSlidei); }
/// <summary> /// Inserts this slide after a given target slide. /// </summary> /// <param name="newSlide">The new slide to insert.</param> /// <param name="prevSlide">The previous slide.</param> /// <remarks> /// This slide will be inserted after the slide specified as a parameter. /// <see href="http://startbigthinksmall.wordpress.com/2011/05/17/cloning-a-slide-using-open-xml-sdk-2-0/">Cloning a Slide using Open Xml SDK 2.0</see> /// </remarks> public static void InsertAfter(PowerpointSlide newSlide, PowerpointSlide prevSlide) { // Find the presentationPart var presentationPart = prevSlide.presentationPart; SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the slide id where to insert our slide SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { // See http://openxmldeveloper.org/discussions/development_tools/f/17/p/5302/158602.aspx if (slideId.RelationshipId == presentationPart.GetIdOfPart(prevSlide.slidePart)) { prevSlideId = slideId; break; } } // Find the highest id uint maxSlideId = slideIdList.ChildElements.Cast <SlideId>().Max(x => x.Id.Value); // public override T InsertAfter<T>(T newChild, DocumentFormat.OpenXml.OpenXmlElement refChild) // Inserts the specified element immediately after the specified reference element. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId + 1; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlide.slidePart); }
// Move a slide to a different position in the slide order in the presentation. public static void MoveSlide(PresentationDocument presentationDocument, int from, int to) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } // Call the CountSlides method to get the number of slides in the presentation. int slidesCount = CountSlides(presentationDocument); // Verify that both from and to positions are within range and different from one another. if (from < 0 || from >= slidesCount) { throw new ArgumentOutOfRangeException("from"); } if (to < 0 || from >= slidesCount || to == from) { throw new ArgumentOutOfRangeException("to"); } // 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 = null; // Identify the position of the target slide after which to move the source slide. if (to == 0) { targetSlide = null; } if (from < to) { targetSlide = slideIdList.ChildElements[to] as SlideId; } else { targetSlide = slideIdList.ChildElements[to - 1] as SlideId; } // Remove the source slide from its current position. sourceSlide.Remove(); // Insert the source slide at its new position after the target slide. slideIdList.InsertAfter(sourceSlide, targetSlide); // Save the modified presentation. presentation.Save(); }
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 SlidePart CloneInputSlide() { if (Presentation == null) { throw new ArgumentNullException("presentationDocument"); } PresentationPart presentationPart = Presentation.PresentationPart; SlideId slideId = presentationPart.Presentation.SlideIdList.GetFirstChild <SlideId>(); string relId = slideId.RelationshipId; // Get the slide part by the relationship ID. SlidePart inputSlide = (SlidePart)presentationPart.GetPartById(relId); if (inputSlide == default(SlidePart)) { throw new ArgumentException("SlidePart"); } //Create a new slide part in the presentation. SlidePart newSlidePart = presentationPart.AddNewPart <SlidePart>("OutPutSlideResult-" + SlideRef); SlideRef++; //Add the slide template content into the new slide. newSlidePart.FeedData(inputSlide.GetStream(FileMode.Open)); //Make sure the new slide references the proper slide layout. newSlidePart.AddPart(inputSlide.SlideLayoutPart); //Get the list of slide ids. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; //Deternmine where to add the next slide (find max number of slides). uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideID in slideIdList.ChildElements) { if (slideID.Id > maxSlideId) { maxSlideId = slideID.Id; prevSlideId = slideID; } } maxSlideId++; //Add the new slide at the end of the deck. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); //Make sure the id and relid are set appropriately. newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); return(newSlidePart); }
public bool CloneSlidePart(int i) { using (PresentationDocument ppt = PresentationDocument.Open(HttpContext.Current.Server.MapPath(folder), true)) { PresentationPart presentationPart = ppt.PresentationPart; OpenXmlElementList slideIds = presentationPart.Presentation.SlideIdList.ChildElements; string relId = (slideIds[0] as SlideId).RelationshipId; // uint max = (slideIds[0] as SlideId).Id; uint k = 0; for (int j = 0; j < i; j++) { k = (slideIds[j] as SlideId).Id; if (k > max) { max = k; } } SlidePart slideTemplate = (SlidePart)presentationPart.GetPartById(relId); SlidePart newSlidePart = presentationPart.AddNewPart <SlidePart>("newSlide" + max); newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open)); newSlidePart.AddPart(slideTemplate.SlideLayoutPart); SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; prevSlideId = slideId; } } maxSlideId++; SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); if (newSlidePart != null) { return(true); } else { return(false); } } }
/// <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 Slide InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideTitle == null) { throw new ArgumentNullException("slideTitle"); } PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); uint drawingObjectId = 1; // Construct the slide content. // Specify the non-visual properties of the new slide. NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); string str = new StreamReader(slidePart.GetStream()).ReadToEnd(); return(slidePart.Slide); // Save the modified presentation. //presentationPart.Presentation.Save(); }
// Insert the specified slide into the presentation at the specified position. public static void InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideTitle == null) { throw new ArgumentNullException("slideTitle"); } PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); uint drawingObjectId = 1; // Construct the slide content. // Specify the non-visual properties of the new slide. NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); // Declare and instantiate the title shape of the new slide. Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); drawingObjectId++; // Specify the required shape properties for the title shape. titleShape.NonVisualShapeProperties = new NonVisualShapeProperties (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" }, new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })); titleShape.ShapeProperties = new ShapeProperties(); // Specify the text of the title shape. titleShape.TextBody = new TextBody(new Drawing.BodyProperties(), new Drawing.ListStyle(), new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle }))); // Declare and instantiate the body shape of the new slide. Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape()); drawingObjectId++; // Specify the required shape properties for the body shape. bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content Placeholder" }, new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 })); bodyShape.ShapeProperties = new ShapeProperties(); // Specify the text of the body shape. bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(), new Drawing.ListStyle(), new Drawing.Paragraph()); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. presentationPart.Presentation.Save(); }
// Insert the specified slide into the presentation at the specified position. public static Slide InsertNewSlide(PresentationDocument presentationDocument, int position, string slideTitle, FileInfo ChartFile) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideTitle == null) { throw new ArgumentNullException("slideTitle"); } PresentationPart presentationPart = presentationDocument.PresentationPart; // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); //uint drawingObjectId = 1; // Construct the slide content. // Specify the non-visual properties of the new slide. NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties()); nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" }; nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties(); nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); //// Declare and instantiate the title shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; //// Specify the required shape properties for the title shape. //titleShape.NonVisualShapeProperties = new NonVisualShapeProperties // (new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Title" }, // new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), // new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })); //titleShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); //// Specify the text of the title shape. //titleShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph(new Drawing.Run(new Drawing.Text() { Text = slideTitle }))); //// Declare and instantiate the body shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape bodyShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; //// Specify the required shape properties for the body shape. //bodyShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "Content Placeholder" }, // new NonVisualShapeDrawingProperties(new Drawing.ShapeLocks() { NoGrouping = true }), // new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Index = 1 })); //bodyShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); //// Specify the text of the body shape. //bodyShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph()); // Declare and instantiate the body shape of the new slide. //DocumentFormat.OpenXml.Presentation.Shape ImgShape = slide.CommonSlideData.ShapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape()); //drawingObjectId++; // Specify the required shape properties for the body shape. //ImgShape.NonVisualShapeProperties = new NonVisualShapeProperties(new NonVisualDrawingProperties() { Id = drawingObjectId, Name = "" }, // new NonVisualGroupShapeDrawingProperties(), // new ApplicationNonVisualDrawingProperties()); //ImgShape.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); // Specify the text of the body shape. //ImgShape.TextBody = new TextBody(new Drawing.BodyProperties(), // new Drawing.ListStyle(), // new Drawing.Paragraph()); //Drawing.Picture picture = new Drawing.Picture(); ////string embedId = string.Empty; //string embedId = "rId" + (slide.Elements().Count() + 915).ToString(); //Drawing.NonVisualPictureProperties nonVisualPictureProperties = new Drawing.NonVisualPictureProperties( // new Drawing.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture" + ChartFile.Name }, // new Drawing.NonVisualPictureDrawingProperties(new Drawing.PictureLocks() { NoChangeAspect = true }), // new ApplicationNonVisualDrawingProperties()); //BlipFill blipFill1 = new BlipFill(); //Drawing.Blip blip1 = new Drawing.Blip() { Embed = embedId, CompressionState = Drawing.BlipCompressionValues.Print }; //// Creates an BlipExtensionList instance and adds its children //Drawing.BlipExtensionList blipExtensionList = new Drawing.BlipExtensionList(); //Drawing.BlipExtension blipExtension = new Drawing.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; //A14.UseLocalDpi useLocalDpi = new A14.UseLocalDpi() { Val = false }; //useLocalDpi.AddNamespaceDeclaration("a14", // "http://schemas.microsoft.com/office/drawing/2010/main"); //blipExtension.Append(useLocalDpi); //blipExtensionList.Append(blipExtension); //blip1.Append(blipExtensionList); //Drawing.Stretch stretch1 = new Drawing.Stretch(); //Drawing.FillRectangle fillRectangle1 = new Drawing.FillRectangle(); //stretch1.Append(fillRectangle1); //blipFill1.Append(blip1); //blipFill1.Append(stretch1); //Drawing.ShapeProperties shapeProperties = new Drawing.ShapeProperties(); //Drawing.Transform2D transform2D = new Drawing.Transform2D(); //Drawing.Offset offset = new Drawing.Offset() { X = 0L, Y = 0L }; //Drawing.Extents extents = new Drawing.Extents() { Cx = 8000000L, Cy = 6000000L }; //transform2D.Append(offset); //transform2D.Append(extents); //Drawing.PresetGeometry presetGeometry = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle }; //Drawing.AdjustValueList adjustValueList = new Drawing.AdjustValueList(); //presetGeometry.Append(adjustValueList); //shapeProperties.Append(transform2D); //shapeProperties.Append(presetGeometry); //picture.Append(nonVisualPictureProperties); //picture.Append(blipFill1); //picture.Append(shapeProperties); //slide.CommonSlideData.ShapeTree.AppendChild(picture); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. //presentationPart.Presentation.Save(); return(GetSlideByRelationShipId(presentationPart, newSlideId.RelationshipId)); }
// Insert the specified slide into the presentation at the specified position. public void InsertNewSlide(PresentationPart presentationPart) { // Verify that the presentation is not empty. if (presentationPart == null) { throw new InvalidOperationException("The presentation document is empty."); } // Declare and instantiate a new slide. Slide slide = new Slide(new CommonSlideData(new P.ShapeTree())); // Specify the group shape properties of the new slide. slide.CommonSlideData.ShapeTree.AppendChild(new P.GroupShapeProperties()); // Create the slide part for the new slide. SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); // Save the new slide part. slide.Save(slidePart); // Modify the slide ID list in the presentation part. // The slide ID list should not be null. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = Convert.ToUInt32(slideIdList.ChildElements.Count()); int position = slideIdList.ChildElements.Count(); SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; } position--; if (position == 0) { prevSlideId = slideId; } } maxSlideId++; // Get the ID of the previous slide. SlidePart lastSlidePart; if (prevSlideId != null) { lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId); } else { lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId); } // Use the same slide layout as that of the previous slide. if (null != lastSlidePart.SlideLayoutPart) { slidePart.AddPart(lastSlidePart.SlideLayoutPart); } // Insert the new slide into the slide list after the previous slide. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); // Save the modified presentation. presentationPart.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); }
public ISlide InsertSlide(ISlideLayout slideLayout, int index) { if (!(slideLayout is IOpenXmlSlideLayout internalSlideLayout)) { throw new ArgumentException(); } Slide slide = new Slide() { // CommonSlideData = slideLayoutRef.SlideLayoutPart.SlideLayout.CommonSlideData.CloneNode(true) as CommonSlideData CommonSlideData = new CommonSlideData() { ShapeTree = new ShapeTree() { NonVisualGroupShapeProperties = internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree.NonVisualGroupShapeProperties.CloneNode(true) as NonVisualGroupShapeProperties, GroupShapeProperties = internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree.GroupShapeProperties.CloneNode(true) as GroupShapeProperties } } }; slide.CommonSlideData.ShapeTree.Append( internalSlideLayout.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree .Select(element => OpenXmlVisualFactory.TryCreateVisual(internalSlideLayout, element, out IOpenXmlVisual visual) ? visual : null).Where(visual => visual != null) .Where(visual => visual?.IsPlaceholder ?? false) .Select(visual => visual.CloneForSlide()) ); SlidePart slidePart = this.presentationPart.AddNewPart <SlidePart>(); slide.Save(slidePart); slidePart.CreateRelationshipToPartDefaultId(internalSlideLayout.SlideLayoutPart); if (this.presentationPart.Presentation.SlideIdList == null) { this.presentationPart.Presentation.SlideIdList = new SlideIdList(); } SlideIdList slideIdList = this.presentationPart.Presentation.SlideIdList; SlideId refSlideId = slideIdList.Elements <SlideId>().Take(index).LastOrDefault(); uint id = refSlideId?.Id ?? 256; SlideId slideId; if (refSlideId != null) { slideId = slideIdList.InsertAfter( new SlideId() { Id = ++id, RelationshipId = presentationPart.GetIdOfPart(slidePart) }, refSlideId ); } else { slideId = slideIdList.PrependChild( new SlideId() { Id = ++id, RelationshipId = presentationPart.GetIdOfPart(slidePart) } ); } for (slideId = slideId.NextSibling <SlideId>(); slideId != null; slideId = slideId.NextSibling <SlideId>()) { slideId.Id = ++id; } return(new OpenXmlSlide(this, slidePart)); }