private Slide insertSlide(PresentationPart presentationPart, string layoutName) { UInt32 slideId = 256U; var slideIdList = presentationPart.Presentation.SlideIdList; SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First(); SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.First(); if (slideIdList == null) { presentationPart.Presentation.SlideIdList = new SlideIdList(); slideIdList = presentationPart.Presentation.SlideIdList; } slideId += Convert.ToUInt32(slideIdList.Count()); Slide slide = new Slide(new CommonSlideData(new ShapeTree())); SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); slide.Save(slidePart); slidePart.AddPart <SlideLayoutPart>(slideLayoutPart); slidePart.Slide.CommonSlideData = (CommonSlideData)slideLayoutPart.SlideLayout.CommonSlideData.Clone(); SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild <SlideId>(new SlideId()); newSlideId.Id = slideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); return(getSlideByRelationShipId(presentationPart, newSlideId.RelationshipId)); }
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); }
public static void SetSlideID(PresentationPart presentationPart, SlidePart slidePart1) { // Insert the new slide into the slide list after the previous slide. 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; prevSlideId = slideId; } } maxSlideId++; SlideId newSlideId = slideIdList.AppendChild(new SlideId()); newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart1); }
internal static Slide InsertSlide(this PresentationPart presentationPart, string layoutName) { UInt32 slideId = 256U; slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count()); Slide slide = new Slide(new CommonSlideData(new ShapeTree())); SlidePart sPart = presentationPart.AddNewPart <SlidePart>(); slide.Save(sPart); SlideMasterPart smPart = presentationPart.SlideMasterParts.First(); SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault (sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)); if (slPart == null) { throw new Exception("The slide layout " + layoutName + " is not found"); } sPart.AddPart <SlideLayoutPart>(slPart); sPart.Slide.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.SingleOrDefault( sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone(); SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild <SlideId>(new SlideId()); newSlideId.Id = slideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart); return(GetSlideByRelationshipId(presentationPart, newSlideId.RelationshipId)); }
private static string AppendSlide(PresentationPart presentationPart, SlidePart newSlidePart) { //MMS:3 //get slides id list SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // find the highest id uint maxSlideId = slideIdList.ChildElements .Cast <SlideId>() .Max(x => x.Id.Value); //create new slide id based on max id uint newId = maxSlideId + 1; //add new slide id item at the second place in the list SlideId newSlideId = new SlideId(); slideIdList.InsertAt(newSlideId, 1); newSlideId.Id = newId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); //newSlideId.RelationshipId.Value = "rId2"; return(newSlideId.RelationshipId); }
public void MergePresentationsSlides(string sourceFolderLocation, string sourcePresentation, string destinationFolderLocation, string destPresentation) { int id = 0; // Open Destination presentation using (PresentationDocument myDestDeck = PresentationDocument.Open(destinationFolderLocation + destPresentation, true)) { PresentationPart destPresPart = myDestDeck.PresentationPart; if (destPresPart.Presentation.SlideIdList == null) { destPresPart.Presentation.SlideIdList = new SlideIdList(); } // Open source presentation using (PresentationDocument mySourceDeck = PresentationDocument.Open(sourceFolderLocation + sourcePresentation, false)) { PresentationPart sourcePresPart = mySourceDeck.PresentationPart; uint uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList); uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList); foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList) { SlidePart sp; SlidePart destSp; SlideMasterPart destMasterPart; string relId; SlideMasterId newSlideMasterId; SlideId newSlideId; id++; sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId); relId = sourcePresentation.Remove(sourcePresentation.IndexOf('.')) + id; destSp = destPresPart.AddPart <SlidePart>(sp, relId); destMasterPart = destSp.SlideLayoutPart.SlideMasterPart; destPresPart.AddPart(destMasterPart); uniqueId++; newSlideMasterId = new SlideMasterId(); newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); newSlideMasterId.Id = uniqueId; maxSlideId++; newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); destPresPart.Presentation.SlideIdList.Append(newSlideId); FixSlideLayoutIds(destPresPart, ref uniqueId); } destPresPart.Presentation.Save(); } } }
//http://stackoverflow.com/questions/32076114/c-sharp-openxml-sdk-2-5-insert-new-slide-from-slide-masters-with-the-layout public static SlidePart AppendNewSlide(PresentationPart presentationPart, SlideLayoutPart masterLayoutPart, out IEnumerable <Shape> placeholderShapes) { Slide clonedSlide = new Slide() { ColorMapOverride = new ColorMapOverride { MasterColorMapping = new Draw.MasterColorMapping() } }; SlidePart clonedSlidePart = presentationPart.AddNewPart <SlidePart>(); clonedSlidePart.Slide = clonedSlide; clonedSlidePart.AddPart(masterLayoutPart); clonedSlide.Save(clonedSlidePart); var masterShapeTree = masterLayoutPart.SlideLayout.CommonSlideData.ShapeTree; placeholderShapes = (from s in masterShapeTree.ChildElements <Shape>() where s.NonVisualShapeProperties.OfType <ApplicationNonVisualDrawingProperties>().Any(anvdp => anvdp.PlaceholderShape != null) select new Shape() { NonVisualShapeProperties = (NonVisualShapeProperties)s.NonVisualShapeProperties.CloneNode(true), TextBody = new TextBody(s.TextBody.ChildElements <Draw.Paragraph>().Select(p => p.CloneNode(true))) { BodyProperties = new Draw.BodyProperties(), ListStyle = new Draw.ListStyle() }, ShapeProperties = new ShapeProperties() }).ToList(); clonedSlide.CommonSlideData = new CommonSlideData { ShapeTree = new ShapeTree(placeholderShapes) { GroupShapeProperties = (GroupShapeProperties)masterShapeTree.GroupShapeProperties.CloneNode(true), NonVisualGroupShapeProperties = (NonVisualGroupShapeProperties)masterShapeTree.NonVisualGroupShapeProperties.CloneNode(true) } }; SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // Find the highest slide ID in the current list. uint maxSlideId = slideIdList.Max(c => (uint?)((SlideId)c).Id) ?? 256; // Insert the new slide into the slide list after the previous slide. slideIdList.Append(new SlideId() { Id = ++maxSlideId, RelationshipId = presentationPart.GetIdOfPart(clonedSlidePart) }); //presentationPart.Presentation.Save(); return(clonedSlidePart); }
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); }
private static void AddSlideMasterToSlideMasterIdList(PresentationPart presentationPart, SlideMasterPart slideMasterPart) { Presentation presentation = presentationPart.Presentation; SlideMasterIdList slideMasterIdList = presentationPart.Presentation.SlideMasterIdList; slideMasterIdList.AppendChild(new SlideMasterId() { Id = slideMasterIdList.Elements <SlideMasterId>().Select(sm => sm.Id.Value).DefaultIfEmpty(2147483647u).Max() + 1, RelationshipId = presentationPart.GetIdOfPart(slideMasterPart) }); presentation.Save(presentationPart); }
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); } } }
public static void AppendSlide(this PresentationPart presentationPart, SlidePart newSlidePart) { SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; // find the highest id uint maxSlideId = slideIdList.ChildElements .Cast <SlideId>() .Max(x => x.Id.Value); // Insert the new slide into the slide list after the previous slide. var id = maxSlideId + 1; SlideId newSlideId = new SlideId(); slideIdList.Append(newSlideId); newSlideId.Id = id; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); }
internal static void ApplyTheme(PresentationPart presentationPart, Drawing.Theme theme) { string themeId = null; if (presentationPart.ThemePart != null) { themeId = presentationPart.GetIdOfPart(presentationPart.ThemePart); presentationPart.DeletePart(themeId); } else { themeId = presentationPart.GetNextRelationshipId(); } ThemePart themePart = presentationPart.AddNewPart <ThemePart>(themeId); theme.Save(themePart); }
internal static void ApplyTableStyles(PresentationPart presentationPart, Drawing.TableStyleList tableStyleList) { string tableStylesId = null; if (presentationPart.TableStylesPart != null) { tableStylesId = presentationPart.GetIdOfPart(presentationPart.TableStylesPart); presentationPart.DeletePart(tableStylesId); } else { tableStylesId = presentationPart.GetNextRelationshipId(); } TableStylesPart tableStylesPart = presentationPart.AddNewPart <TableStylesPart>(tableStylesId); tableStyleList.Save(tableStylesPart); }
/// <summary> /// Insert a new Slide into PowerPoint /// </summary> /// <param name="presentationPart">Presentation Part</param> /// <param name="layoutName">Layout of the new Slide</param> /// <returns>Slide Instance</returns> public Slide InsertSlide(PresentationPart presentationPart, string layoutName) { UInt32 slideId = 256U; // Get the Slide Id collection of the presentation document var slideIdList = presentationPart.Presentation.SlideIdList; if (slideIdList == null) { throw new NullReferenceException("The number of slide is empty, please select a ppt with a slide at least again"); } slideId += Convert.ToUInt32(slideIdList.Count()); // Creates a Slide instance and adds its children. Slide slide = new Slide(new CommonSlideData(new ShapeTree())); SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(); slide.Save(slidePart); // Get SlideMasterPart and SlideLayoutPart from the existing Presentation Part SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First(); SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault (sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName, StringComparison.OrdinalIgnoreCase)); if (slideLayoutPart == null) { throw new Exception("The slide layout " + layoutName + " is not found"); } slidePart.AddPart <SlideLayoutPart>(slideLayoutPart); slidePart.Slide.CommonSlideData = (CommonSlideData)slideMasterPart.SlideLayoutParts.SingleOrDefault( sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone(); // Create SlideId instance and Set property SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild <SlideId>(new SlideId()); newSlideId.Id = slideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart); return(GetSlideByRelationShipId(presentationPart, newSlideId.RelationshipId)); }
internal static Slide InsertSlide(this PresentationPart presentationPart, string layoutName) { //1) create the slide Slide slide = new Slide(new CommonSlideData(new ShapeTree())); ////2) specify 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(); ////3) Specify the group shape properties of the new slide. //slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties()); //4) create slide part for the new slide inside the presentation part SlidePart sPart = presentationPart.AddNewPart <SlidePart>(); slide.Save(sPart); //5) set the slidelayout SlideMasterPart smPart = presentationPart.SlideMasterParts.First(); SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)); sPart.AddPart <SlideLayoutPart>(slPart); //sPart.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.SingleOrDefault( // sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone(); //6) set the slideid UInt32 slideId = 256U; slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count()); SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild <SlideId>(new SlideId()); newSlideId.Id = slideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart); return(GetSlideByRelationshipId(presentationPart, newSlideId.RelationshipId)); }
// 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(); }
// Apply a new theme to the presentation. public static void ApplyThemeToPresentation(PresentationDocument presentationDocument, PresentationDocument themeDocument) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (themeDocument == null) { throw new ArgumentNullException("themeDocument"); } // Get the presentation part of the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the existing slide master part. SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.ElementAt(0); string relationshipId = presentationPart.GetIdOfPart(slideMasterPart); // Get the new slide master part. SlideMasterPart newSlideMasterPart = themeDocument.PresentationPart.SlideMasterParts.ElementAt(0); // Remove the existing theme part. presentationPart.DeletePart(presentationPart.ThemePart); // Remove the old slide master part. presentationPart.DeletePart(slideMasterPart); // Import the new slide master part, and reuse the old relationship ID. newSlideMasterPart = presentationPart.AddPart(newSlideMasterPart, relationshipId); // Change to the new theme part. presentationPart.AddPart(newSlideMasterPart.ThemePart); Dictionary <string, SlideLayoutPart> newSlideLayouts = new Dictionary <string, SlideLayoutPart>(); foreach (var slideLayoutPart in newSlideMasterPart.SlideLayoutParts) { newSlideLayouts.Add(GetSlideLayoutType(slideLayoutPart), slideLayoutPart); } string layoutType = null; SlideLayoutPart newLayoutPart = null; // Insert the code for the layout for this example. string defaultLayoutType = "Title and Content"; // Remove the slide layout relationship on all slides. foreach (var slidePart in presentationPart.SlideParts) { 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); } if (layoutType != null && newSlideLayouts.TryGetValue(layoutType, out newLayoutPart)) { // Apply the new layout part. slidePart.AddPart(newLayoutPart); } else { newLayoutPart = newSlideLayouts[defaultLayoutType]; // Apply the new default layout part. slidePart.AddPart(newLayoutPart); } } }
/// <summary> /// Returns a string array of the text in the presentation slides. /// </summary> /// <param name="presentationPart">The presentation part of the presentation document.</param> /// <returns>string array of the text in the presentation slides.</returns> private string[] GetPresentationSlidesText(PresentationPart presentationPart) { var presentation = presentationPart.Presentation; var slideParts = presentationPart.SlideParts.ToList(); var retVal = new string[slideParts.Count()]; var relationshipId = string.Empty; var tempSlideNumber = 0; foreach (var slidePart in slideParts) { var slide = presentation.SlideIdList.Where(s => ((SlideId)s).RelationshipId == presentationPart.GetIdOfPart(slidePart)).FirstOrDefault(); var index = presentation.SlideIdList.ToList().IndexOf(slide); relationshipId = ((SlideId)presentation.SlideIdList.ChildElements[index]).RelationshipId; var titles = new List <string>(); var content = new List <string>(); var notes = new List <string>(); slidePart.Slide.Descendants <Shape>().ToList().ForEach(shape => { foreach (var item in shape.Descendants <PlaceholderShape>().Where(i => i.Type != null)) { if ((item.Type.ToString().ToUpper() == "CenteredTitle".ToUpper() || item.Type.ToString().ToUpper() == "SubTitle".ToUpper() || item.Type.ToString().ToUpper() == "Title".ToUpper()) && shape.TextBody != null && !string.IsNullOrWhiteSpace(shape.TextBody.InnerText)) { titles.AddRange(shape.TextBody.Descendants <DocumentFormat.OpenXml.Drawing.Text>().Select(s => s.Text)); } } }); content = slidePart.Slide.Descendants <DocumentFormat.OpenXml.Drawing.Text>().Select(s => s.Text).ToList(); content.RemoveAll(s => titles.Any(t => t == s)); if (slidePart.NotesSlidePart != null && slidePart.NotesSlidePart.NotesSlide != null && slidePart.NotesSlidePart.NotesSlide.Descendants() != null && slidePart.NotesSlidePart.NotesSlide.Descendants().Count() > 0) { notes = slidePart.NotesSlidePart.NotesSlide.Descendants <DocumentFormat.OpenXml.Drawing.Text>().Where(s => { return(!int.TryParse(s.Text, out tempSlideNumber)); // Remove the record as it contains the slide number. }).Select(s => s.Text).ToList(); } retVal[index] = string.Join(string.Empty, string.Join(Environment.NewLine, titles.ToArray()), string.Join(string.Empty, content.ToArray()), string.Join(Environment.NewLine, notes.ToArray())); } return(retVal); }
public void MergeSlides(string presentationFolder, string sourcePresentation, string destPresentation, int id, int Index) { int Idx = 0; // Open the destination presentation. using (PresentationDocument myDestDeck = PresentationDocument.Open(presentationFolder + destPresentation, true)) { PresentationPart destPresPart = myDestDeck.PresentationPart; // If the merged presentation does not have a SlideIdList // element yet, add it. if (destPresPart.Presentation.SlideIdList == null) { destPresPart.Presentation.SlideIdList = new SlideIdList(); } // Open the source presentation. This will throw an exception if // the source presentation does not exist. using (PresentationDocument mySourceDeck = PresentationDocument.Open(presentationFolder + sourcePresentation, false)) { PresentationPart sourcePresPart = mySourceDeck.PresentationPart; // Get unique ids for the slide master and slide lists for use later. uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList); uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList); // Copy each slide in the source presentation, in order, to the destination presentation. foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList) { if (Idx++ == Index) { SlidePart sp; SlidePart destSp; SlideMasterPart destMasterPart; string relId; SlideMasterId newSlideMasterId; SlideId newSlideId; // Create a unique relationship id. id++; sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId); relId = sourcePresentation.Remove(sourcePresentation.IndexOf('.')) + id; // Add the slide part to the destination presentation. destSp = destPresPart.AddPart <SlidePart>(sp, relId); // The slide master part was added. Make sure the // relationship between the main presentation part and // the slide master part is in place. destMasterPart = destSp.SlideLayoutPart.SlideMasterPart; destPresPart.AddPart(destMasterPart); // Add the slide master id to the slide master id list. uniqueId++; newSlideMasterId = new SlideMasterId(); newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); newSlideMasterId.Id = uniqueId; destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); // Add the slide id to the slide id list. maxSlideId++; newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; destPresPart.Presentation.SlideIdList.Append(newSlideId); } } // Make sure that all slide layout ids are unique. FixSlideLayoutIds(destPresPart); } // Save the changes to the destination deck. destPresPart.Presentation.Save(); } }
// 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 SlideTagExCollection GetSlideTags(PresentationPart presentationPart) { TagList tagList = null; SlideTagExCollection slideTagList = new SlideTagExCollection(); foreach (var sldPart in presentationPart.SlideParts) { var slide = presentationPart.Presentation.SlideIdList.Where(s => ((SlideId)s).RelationshipId == presentationPart.GetIdOfPart(sldPart)).FirstOrDefault(); int index = presentationPart.Presentation.SlideIdList.ToList().IndexOf(slide); foreach (UserDefinedTagsPart tagPart in sldPart.UserDefinedTagsParts) { tagList = tagPart.TagList; foreach (Tag tag in tagList.ChildElements) { if (tag.Name.Value.Equals(PL_TAG)) { SlideTagEx slideTagEx = null; slideTagEx = XmlUtils.Deserialize <SlideTagEx>(tag.Val.ToString()); slideTagEx.PptSlideIndex = ++index; slideTagEx.Title = sldPart.Slide.InnerText; if (slideTagEx != null) { slideTagList.Add(slideTagEx); } } } } } return(slideTagList); }
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 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)); }
private void MergeSlides(PresentationInfo sourcePresentation, string destPresentation, ref Dictionary <int, int> reOrderPair, ref int val, out int reOrderConstantValue) { using (PresentationDocument destinationPresentationDoc = PresentationDocument.Open(destPresentation, true)) { PresentationPart destPresPart = destinationPresentationDoc.PresentationPart; // If the merged presentation doesn't have a SlideIdList element yet then add it. if (destPresPart.Presentation.SlideIdList == null) { destPresPart.Presentation.SlideIdList = new SlideIdList(); } string sourceFileName = GetFileNameWithExtension(sourcePresentation.File); // Open the source presentation. This will throw an exception if the source presentation does not exist. using (PresentationDocument sourcePresentationDoc = PresentationDocument.Open(sourceFileName, false)) { PresentationPart sourcePresPart = sourcePresentationDoc.PresentationPart; // Get unique ids for the slide master and slide lists for use later. uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList); uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList); reOrderConstantValue = sourcePresPart.Presentation.SlideIdList.Count(); // Copy each slide in the source presentation in order to the destination presentation. foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList) { SlidePart sp; SlidePart destSp; SlideMasterPart destMasterPart; string relId; SlideMasterId newSlideMasterId; SlideId newSlideId; //increase the slide count mergedSideCount++; if (sourcePresentation.InsertPosition != -1) { reOrderPair.Add(mergedSideCount - 1, val++); } // Create a unique relationship id. id++; sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId); relId = "uniq" + id; // Add the slide part to the destination presentation. destSp = destPresPart.AddPart <SlidePart>(sp, relId); // The master part was added. Make sure the relationship is in place. destMasterPart = destSp.SlideLayoutPart.SlideMasterPart; destPresPart.AddPart(destMasterPart); // Add slide master to slide master list. uniqueId++; newSlideMasterId = new SlideMasterId(); newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); newSlideMasterId.Id = uniqueId; // Add slide to slide list. maxSlideId++; newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); destPresPart.Presentation.SlideIdList.Append(newSlideId); } // Make sure all slide ids are unique. ModifySlideLayoutIds(destPresPart); } // Save the changes to the destination presentation. destPresPart.Presentation.Save(); } }