public ActionResult DownLoadATCPPT(int ATSID) { string ExportFileName = "test.pptx"; try { System.IO.File.Copy(Server.MapPath("~/ATSchedule/") + "Sample.pptx", Server.MapPath("~/ATSchedule/") + "SampleTemp.pptx", true); using (PresentationDocument prstDoc = PresentationDocument.Open(Server.MapPath("~/ATSchedule/") + "SampleTemp.pptx", true)) { using (tsmc14BDataContext db = new tsmc14BDataContext()) { var ATSLast = db.vw_ATScheduleLastHistory.Where(x => x.ATSID == ATSID).SingleOrDefault(); if (ATSLast != null) { string ATSFolderString = Server.MapPath("~/ATSchedule/") + ATSID + "/" + ATSLast.FolderName; System.IO.DirectoryInfo ATSFolder = new System.IO.DirectoryInfo(ATSFolderString); if (ATSFolder.Exists) { FileInfo[] ATSFiles = ATSFolder.GetFiles(); for (int i = 0; i < ATSFiles.Count(); i++) { string imgId = "rId" + ATSFiles[i].Name; Slide S = InsertNewSlide(prstDoc, i, imgId, ATSFiles[i]); InsertImageInLastSlide(S, ATSFiles[i].FullName, "image/png"); } } } else { return(null); } } SlideIdList slideIdList = prstDoc.PresentationPart.Presentation.SlideIdList; SlideId slideId = slideIdList.ChildElements[slideIdList.Count() - 1] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); SlidePart slidePart = prstDoc.PresentationPart.GetPartById(slideRelId) as SlidePart; prstDoc.PresentationPart.DeletePart(slidePart); prstDoc.PresentationPart.Presentation.Save(); return(File(Server.MapPath("~/ATSchedule/") + "SampleTemp.pptx", "application/mspowerpoint", ExportFileName)); } } catch (Exception ex) { MemoryStream fs = new MemoryStream(); TextWriter tx = new StreamWriter(fs); tx.WriteLine(ex.Message); tx.Flush(); return(File(fs.ToArray(), "text/text", "error.txt")); } }
public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex, int slidesCount) { slideIndex--; if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } if (slideIndex < 0 || slideIndex >= slidesCount) { throw new ArgumentOutOfRangeException("slideIndex"); } PresentationPart presentationPart = presentationDocument.PresentationPart; Presentation presentation = presentationPart.Presentation; SlideIdList slideIdList = presentation.SlideIdList; SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); if (presentation.CustomShowList != null) { foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList != null) { LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } } presentation.Save(); SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; presentationPart.DeletePart(slidePart); }
/// <summary> /// Delete the specified slide from the presentation by slide index /// </summary> /// <param name="presentationPart"></param> /// <param name="slideIndex"></param> private static void DeleteSlide(PresentationPart presentationPart, int slideIndex) { // Get the presentation from the presentation part. Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // 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 (CustomShow 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); } } } } // Get the slide part for the specified slide. SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; // Remove the slide part. presentationPart.DeletePart(slidePart); }
private void RemoveFromDom(int number) { PresentationPart presentationPart = _sdkPre.PresentationPart; DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation; // gets the list of slide identifiers in the presentation SlideIdList slideIdList = presentation.SlideIdList; // gets the slide identifier of the specified slide SlideId slideId = (SlideId)slideIdList.ChildElements[number - 1]; // gets the relationship identifier of the slide string slideRelId = slideId.RelationshipId; // removes the slide from the slide list slideIdList.RemoveChild(slideId); // remove references to the slide from all custom shows if (presentation.CustomShowList != null) { // iterates through the list of custom shows foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>()) { if (customShow.SlideList == null) { continue; } // declares a link list of slide list entries var slideListEntries = new LinkedList <SlideListEntry>(); foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements()) { // finds the slide reference to remove from the custom show if (slideListEntry.Id != null && slideListEntry.Id == slideRelId) { slideListEntries.AddLast(slideListEntry); } } // removes all references to the slide from the custom show foreach (SlideListEntry slideListEntry in slideListEntries) { customShow.SlideList.RemoveChild(slideListEntry); } } } // gets the slide part for the specified slide SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; // removes the slide part presentationPart.DeletePart(slidePart); }
/// <summary> /// Removes the slide from the PowerPoint file. /// </summary> /// <remarks> /// <see href="http://msdn.microsoft.com/en-us/library/office/cc850840.aspx">How to: Delete a Slide from a Presentation</see> /// </remarks> public void Remove() { SlideIdList slideIdList = this.presentationPart.Presentation.SlideIdList; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.RelationshipId == this.presentationPart.GetIdOfPart(this.slidePart)) { slideIdList.RemoveChild(slideId); break; } } this.presentationPart.DeletePart(this.slidePart); }
void DeleteTemplateSlide(PresentationPart presentationPart, SlidePart slideTemplate) { //Get the list of slide ids SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; //Delete the template slide reference foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.RelationshipId.Value.Equals("rId2")) { slideIdList.RemoveChild(slideId); } } //Delete the template slide presentationPart.DeletePart(slideTemplate); }
private void DeleteTemplateSlide(SlidePart slidePart, string id) { //delete the template slide and any references SlideIdList slideIdList = _doc.PresentationPart.Presentation.SlideIdList; foreach ( SlideId slideId in slideIdList.ChildElements.Cast <SlideId>() .Where(slideId => slideId.RelationshipId.Value.Equals(id))) { slideIdList.RemoveChild(slideId); } _doc.PresentationPart.DeletePart(slidePart); _doc.PresentationPart.Presentation.Save(); }
// Delete the specified slide from the presentation. public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex) { // Get the presentation part from the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the presentation from the presentation part. Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // Remove the slide from the slide list. slideIdList.RemoveChild(slideId); }
// Delete the specified slide from the presentation. public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } // Use the CountSlides sample to get the number of slides in the presentation. int slidesCount = CountSlides(presentationDocument); if (slideIndex < 0 || slideIndex >= slidesCount) { throw new ArgumentOutOfRangeException("slideIndex"); } // Get the presentation part from the presentation document. PresentationPart presentationPart = presentationDocument.PresentationPart; // Get the presentation from the presentation part. Presentation presentation = presentationPart.Presentation; // Get the list of slide IDs in the presentation. SlideIdList slideIdList = presentation.SlideIdList; // Get the slide ID of the specified slide SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId; // Get the relationship ID of the slide. string slideRelId = slideId.RelationshipId; // 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(); // Get the slide part for the specified slide. SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; // Remove the slide part. presentationPart.DeletePart(slidePart); }
private static void AddImage(string file, List <ImagePath> image) { using (var presentationDocument = PresentationDocument.Open(file, true)) { var slideCount = presentationDocument.PresentationPart.SlideParts.Count(); SlideIdList slideIdList = presentationDocument.PresentationPart.Presentation.SlideIdList; Presentation presentation = presentationDocument.PresentationPart.Presentation; PresentationPart presentationPart = presentationDocument.PresentationPart; OpenXmlElementList slideIds = presentationPart.Presentation.SlideIdList.ChildElements; int cnt = 0; // 画像の添付数 int j = 0; // 画像添付スライド位置 string relId = (slideIds[j] as SlideId).RelationshipId; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId); foreach (ImagePath imgPath in image) { ImagePart part = slidePart .AddImagePart(ImagePartType.Png); using (var stream = File.OpenRead(imgPath.Path)) { part.FeedData(stream); } var tree = slidePart .Slide .Descendants <DocumentFormat.OpenXml.Presentation.ShapeTree>() .First(); var picture = new DocumentFormat.OpenXml.Presentation.Picture(); picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties(); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties { Name = "My Shape", Id = (UInt32)tree.ChildElements.Count - 1 }); var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties(); nonVisualPictureDrawingProperties.Append(new Drawing2.PictureLocks() { NoChangeAspect = true }); picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties); picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties()); var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill(); var blip1 = new Drawing2.Blip() { Embed = slidePart.GetIdOfPart(part) }; var blipExtensionList1 = new Drawing2.BlipExtensionList(); var blipExtension1 = new Drawing2.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi() { Val = false }; useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExtension1.Append(useLocalDpi1); blipExtensionList1.Append(blipExtension1); blip1.Append(blipExtensionList1); var stretch = new Drawing2.Stretch(); stretch.Append(new Drawing2.FillRectangle()); blipFill.Append(blip1); blipFill.Append(stretch); picture.Append(blipFill); picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties(); picture.ShapeProperties.Transform2D = new Drawing2.Transform2D(); int rotation = 0; switch (imgPath.Rot) { case RotateFlipType.RotateNoneFlipNone: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate180FlipNone: // 時計回りに180度回転しているので、180度回転して戻す rotation = 60000 * 180; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; case RotateFlipType.Rotate90FlipNone: // 時計回りに270度回転しているので、90度回転して戻す rotation = 60000 * 90; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; case RotateFlipType.Rotate270FlipNone: // 時計回りに90度回転しているので、270度回転して戻す rotation = 60000 * 270; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION90[cnt % 6].X, Y = POSITION90[cnt % 6].Y, }); break; default: rotation = 0; picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset { X = POSITION0[cnt % 6].X, Y = POSITION0[cnt % 6].Y, }); break; } picture.ShapeProperties.Transform2D.Rotation = rotation; // 縦向き picture.ShapeProperties.Transform2D.Append(new Drawing2.Extents { Cx = imgPath.Size.Width, Cy = imgPath.Size.Height, }); picture.ShapeProperties.Append(new Drawing2.PresetGeometry { Preset = Drawing2.ShapeTypeValues.Rectangle }); tree.Append(picture); if (cnt % 6 == 5) { if (j < slideCount - 1) { j++; relId = (slideIds[j] as SlideId).RelationshipId; slidePart = (SlidePart)presentationPart.GetPartById(relId); } else { // 画像ループを抜ける break; } } cnt++; } // スライドを削除する for (int i = slideCount - 1; i > j; i--) { //Console.WriteLine(i); SlideId slideId = slideIds[i] as SlideId; string slideRelId = slideId.RelationshipId; slideIdList.RemoveChild(slideId); 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); } } } } presentation.Save(); SlidePart slidePart2 = presentationPart.GetPartById(slideRelId) as SlidePart; presentationPart.DeletePart(slidePart2); } } }
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; } } } } }