private void DeleteSlide(int index) { //TODO: try-catch PPT.Application app = new PPT.Application(); PPT.Presentations pres = app.Presentations; PPT.Presentation pptx = pres.Open(LibraryFile.FullPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); PPT.Slides slides = pptx.Slides; PPT.Slide slide = slides[index]; slide.Delete(); pptx.Save(); pptx.Close(); slide.ReleaseCOM(); slide = null; slides.ReleaseCOM(); slides = null; pptx.ReleaseCOM(); pptx = null; pres.ReleaseCOM(); pres = null; app.ReleaseCOM(); app = null; }
public List <IFileItem> AppendShapes(List <PPT.Shape> srcShapes) { PPT.Application ppt = null; PPT.Presentations pres = null; PPT.Presentation pptPresentation = null; try { ppt = new PPT.Application(); pres = ppt.Presentations; pptPresentation = pres.Open(FullPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue); PPT.DocumentWindow wnd = pptPresentation.Windows[1]; //wnd.Height = 200; //wnd.Top = -200; wnd.WindowState = PPT.PpWindowState.ppWindowMinimized; wnd = null; PPT.Slides gSlides = pptPresentation.Slides; int startIndex = gSlides.Count + 1; int count = 0; foreach (PPT.Shape shape in srcShapes) { PPT.Slide slide = gSlides.Add(startIndex + count++, PPT.PpSlideLayout.ppLayoutBlank); shape.Copy(); slide.Shapes.Paste(); slide.Tags.Add(ShapeTag.Tag, ShapeTag.Value); slide.ReleaseCOM(); slide = null; } List <IFileItem> newItems = new List <IFileItem>(); newItems = fileIndex.UpdateIndex(pptPresentation, startIndex); return(newItems); } finally { if (pptPresentation != null) { pptPresentation.Save(); pptPresentation.Close(); } pptPresentation.ReleaseCOM(); pptPresentation = null; pres.ReleaseCOM(); pres = null; ppt.ReleaseCOM(); ppt = null; } }
private void InsertItem(IFileItem item) { if (item == null) { return; } PPT.Application app = new PPT.Application(); PPT.Presentations pres = app.Presentations; PPT.Presentation pptx = pres.Open(item.File.FullPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); PPT.Slides slides = pptx.Slides; PPT.Slide slide = slides[item.Index]; if (item.Type == ItemType.Slide) { slide.Copy(); } else { PPT.Shapes shapes = slide.Shapes; PPT.Shape shape = shapes[1]; shape.Copy(); shapes.ReleaseCOM(); shapes = null; shape.ReleaseCOM(); shape = null; } PPT.Presentation dstpptx = app.ActivePresentation; PPT.DocumentWindow wnd = app.ActiveWindow; PPT.View view = wnd.View; //TODO: Check if there is no selection (selection between slides) PPT.Slides dstSlides = dstpptx.Slides; PPT.Slide dstSlide = null; dstSlide = view.Slide as PPT.Slide; int ix = dstSlide.SlideIndex + 1; if (item.Type == ItemType.Slide) { dstSlide.Copy(); var r = dstSlides.Paste(); //TODO: dstSlides.Paste(ix) Hangs here var s = r[1]; s.MoveTo(ix); s.ReleaseCOM(); s = null; r.ReleaseCOM(); r = null; } else { view.Paste(); } dstSlide.ReleaseCOM(); dstSlide = null; wnd.ReleaseCOM(); wnd = null; view.ReleaseCOM(); view = null; dstpptx.ReleaseCOM(); dstpptx = null; slide.ReleaseCOM(); slide = null; slides.ReleaseCOM(); slides = null; pptx.Close(); pptx.ReleaseCOM(); pptx = null; pres.ReleaseCOM(); pres = null; app.ReleaseCOM(); app = null; dstSlides.ReleaseCOM(); dstSlides = null; }
/// <summary> /// Scans pptx file starting slide with specified index /// <para>Can throw exceptions from inner calls.</para> /// </summary> /// <param name="pptPresentation"></param> /// <param name="startIndex"></param> /// <returns></returns> public List <IFileItem> UpdateIndex(PPT.Presentation pptPresentation, int startIndex) { PPT.Slide slide = null; dynamic addin = null; string tempIndexFile = null; List <string> tempFilesToDelete = new List <string>(); try { List <IFileItem> newItems = new List <IFileItem>(); string tempImgFile = null; addin = GetAddin(); tempIndexFile = Path.GetTempFileName(); using (FileStream zipFile = new FileStream(indexFileFullName, FileMode.Open)) { using (ZipArchive zip = new ZipArchive(zipFile, ZipArchiveMode.Update)) { ZipArchiveEntry indexFileEntry = zip.GetEntry(ZIPEntry_IndexFileName); indexFileEntry.ExtractToFile(tempIndexFile, true); PPT.Slides gSlides = pptPresentation.Slides; for (int idx = startIndex; idx <= gSlides.Count; idx++) { slide = gSlides[idx]; bool isShapeItem = slide.Tags[ShapeTag.Tag] != ""; #region Collect Item Data string title = ""; StringBuilder sbKeyWords = new StringBuilder(); StringBuilder sbChartType = new StringBuilder(); StringBuilder sbDescr = new StringBuilder(); int chartsCount = 0; if (isShapeItem) { try { PPT.Shape shape = slide.Shapes[1]; //If it is the Shape we expect it to be the only shape on this slide if (addin?.IsExcelChart(shape) ?? false) { chartsCount++; #region Get Chart Description string[] str = addin.GetChartDescription(shape); } #endregion Get MG Chart Description else { //TODO: Get something from non chart shape title = shape.Title.Replace("\n", " ").Replace(" ", " "); sbKeyWords.Append(shape.Name); } } catch (Exception ex) { } } else { //Slide foreach (PPT.Shape shape in slide.Shapes) { try { if (shape.Name == "Title 1" && title == "") { PPT.TextFrame frame = shape.TextFrame; PPT.TextRange txt = frame.TextRange; title = txt.Text.Replace("\n", " ").Replace(" ", " "); txt = null; frame = null; } if (addin?.IsExceloChart(shape) ?? false) { chartsCount++; } } catch { } } } sbKeyWords.Append(title); sbKeyWords.Append(" "); tempImgFile = Path.GetTempFileName(); float ratio = pptPresentation.PageSetup.SlideHeight / pptPresentation.PageSetup.SlideWidth; slide.Export(tempImgFile, "png", ThumbnailWidth, (int)(ThumbnailWidth * ratio)); zip.CreateEntryFromFile(tempImgFile, ZIPEntry_ImagesFolder + idx + ".png"); Bitmap img = null; using (var fs = new System.IO.FileStream(tempImgFile, System.IO.FileMode.Open)) { img = new Bitmap(fs); } sbChartType.Replace("\n", " "); #endregion IFileItem item = LibraryFile.CreateItem(idx); { item.Image = img; item.Type = ( (Func <ItemType>)(() => { if (isShapeItem) { return(chartsCount == 0 ? ItemType.Shape : ItemType.Chart); } else { // if (mgChartsCount == 0) { return(ItemType.Slide); } // else // { // return mgChartsCount > 1 ? ItemType.SlideWithMultipleCharts : ItemType.SlideWithChart; // } } }) )(); item.ShapesCount = chartsCount; item.Title = title; item.Description = sbDescr.ToString(); item.Keywords = sbKeyWords.ToString(); //TODO: item.ChartType = sbChartType.ToString(); } newItems.Add(item); slide = null; } items.AddRange(newItems); Save(newItems, tempIndexFile); indexFileEntry.Delete(); zip.CreateEntryFromFile(tempIndexFile, ZIPEntry_IndexFileName); } //using (ZipArchive zip = new ZipArchive(zipFile, ZipArchiveMode.Update)) } //using (FileStream zipFile = new FileStream(indexFilePath, FileMode.Open)) return(newItems); } finally { addin = null; slide.ReleaseCOM(); slide = null; pptPresentation = null; try { if (tempIndexFile != null) { File.Delete(tempIndexFile); } foreach (string path in tempFilesToDelete) { File.Delete(path); } } catch { } } }