/// <summary> /// Clones this slide. /// </summary> /// <returns>The clone.</returns> /// <remarks> /// <see href="http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx">Adding Repeating Data to PowerPoint</see> /// <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> /// <see href="http://www.exsilio.com/blog/post/2011/03/21/Cloning-Slides-including-Images-and-Charts-in-PowerPoint-presentations-Using-Open-XML-SDK-20-Productivity-Tool.aspx">See Cloning Slides including Images and Charts in PowerPoint presentations and Using Open XML SDK 2.0 Productivity Tool</see> /// </remarks> public PowerpointSlide Clone() { SlidePart slideTemplate = this.slidePart; // Clone slide contents SlidePart slidePartClone = this.presentationPart.AddNewPart <SlidePart>(); using (var templateStream = slideTemplate.GetStream(FileMode.Open)) { slidePartClone.FeedData(templateStream); } // Copy layout part slidePartClone.AddPart(slideTemplate.SlideLayoutPart); // Copy the image parts foreach (ImagePart image in slideTemplate.ImageParts) { ImagePart imageClone = slidePartClone.AddImagePart(image.ContentType, slideTemplate.GetIdOfPart(image)); using (var imageStream = image.GetStream()) { imageClone.FeedData(imageStream); } } return(new PowerpointSlide(this.presentationPart, slidePartClone)); }
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 void Create() { NewSlidePart = _doc.PresentationPart.AddNewPart <SlidePart>(NewSlideName + _slideId++); //copy the contents of the template slide to the new slide and attach the appropriate layout NewSlidePart.FeedData(_teplateSlide.GetStream(FileMode.Open)); NewSlidePart.AddPart(_teplateSlide.SlideLayoutPart, _teplateSlide.GetIdOfPart(_teplateSlide.SlideLayoutPart)); }
public static string GetSlideText(SlidePart slidePart) { XDocument xDoc = XDocument.Load(XmlReader.Create(slidePart.GetStream())); if (xDoc == null) { return(""); } return(string.Join("", xDoc.Root.Descendants(powerpointmlNamespace + "t").Select(m => (string)m))); }
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); }
static void Main(string[] args) { WordprocessingDocument doc = WordprocessingDocument.Create(@"C:\tmp\testOpenXmlLib.docx", OpenXmlPackage.DocumentType.Document); MainDocumentPart part = doc.MainDocumentPart; const string docXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> <w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body> </w:document>"; Stream stream = part.GetStream(); byte[] buf = (new UTF8Encoding()).GetBytes(docXml); stream.Write(buf, 0, buf.Length); doc.Close(); PresentationDocument presentation = PresentationDocument.Create(@"C:\tmp\testOpenXmlLib.pptx", OpenXmlPackage.DocumentType.Document); PresentationPart presentationPart = presentation.PresentationPart; SlidePart slide = presentationPart.AddSlidePart(); string presentationXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> <p:presentation xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:p=""http://schemas.openxmlformats.org/presentationml/2006/main"" saveSubsetFonts=""1""> <p:sldIdLst> <p:sldId id=""256"" r:id=""" + slide.RelIdToString + @"""/> </p:sldIdLst> </p:presentation>"; stream = presentationPart.GetStream(); buf = (new UTF8Encoding()).GetBytes(presentationXml); stream.Write(buf, 0, buf.Length); string slideXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> <p:sld xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:p=""http://schemas.openxmlformats.org/presentationml/2006/main""> </p:sld>"; stream = slide.GetStream(); buf = (new UTF8Encoding()).GetBytes(slideXml); stream.Write(buf, 0, buf.Length); presentation.Close(); }
private SlidePart CloneSlidePart(SlidePart ticketSlide) { Slide currSlide = (Slide)ticketSlide.Slide.CloneNode(true); SlidePart sp = document.PresentationPart.AddNewPart <SlidePart>(); currSlide.Save(sp); using (var stream = ticketSlide.GetStream()) { sp.FeedData(stream); sp.AddPart(ticketSlide.SlideLayoutPart); } return(sp); }
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 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(); }