/// <summary> /// Reads one item form current position of the stream assosiated with index file. /// index parameter if used for verification /// </summary> /// <param name="stream"></param> /// <param name="index"></param> /// <returns></returns> protected IFileItem ReadItem(StreamReader stream, int index) { IFileItem row = LibraryFile.CreateItem(index); string str; str = stream.ReadLine(); // 1 if (str == null) { return(null); } int idx = -1; if (!int.TryParse(str, out idx) || idx != index) //Broken structure { // return null; } str = stream.ReadLine(); // 2 if (str == null) { return(null); } str = XmlConvert.DecodeName(str); ItemType type = ItemType.Slide; Enum.TryParse <ItemType>(str, out type); { row.Type = type; } str = stream.ReadLine(); // 3 if (str == null) { return(null); } int.TryParse(str, out int count); { row.ShapesCount = count; } str = stream.ReadLine(); // 4 if (str == null) { return(null); } str = XmlConvert.DecodeName(str); row.Title = str; str = stream.ReadLine(); // 5 if (str == null) { return(null); } str = XmlConvert.DecodeName(str); row.Description = str; str = stream.ReadLine(); // 6 if (str == null) { return(null); } str = XmlConvert.DecodeName(str); row.Keywords = str; str = stream.ReadLine(); // 7 if (str != "### " + index) { //Broken structure return(null); } return(row); }
/// <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 { } } }