public void CreatePowerPoint(CreateFileBindingModel createPPBindingModel) { //Uses CreateFileBindingModel to get specific information from it. //Creates a excel application that runs in the background. //In the background the application adds the things it needs to be opened after being created. PowerPoint.Application objPowerPoint = new PowerPoint.Application(); PowerPoint.Presentation objPresentation = objPowerPoint.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse); Microsoft.Office.Interop.PowerPoint.Slides slides; Microsoft.Office.Interop.PowerPoint._Slide slide; Microsoft.Office.Interop.PowerPoint.TextRange objText; Microsoft.Office.Interop.PowerPoint.CustomLayout custLayout = objPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText]; slides = objPresentation.Slides; slide = slides.AddSlide(1, custLayout); objText = slide.Shapes[1].TextFrame.TextRange; objText.Text = "Title of page"; objText.Font.Name = "Arial"; objText.Font.Size = 32; Microsoft.Office.Interop.PowerPoint.Shape shape = slide.Shapes[2]; //After the user has given a directory and a name for the file, it is saved there with that name. objPresentation.SaveAs(createPPBindingModel.DestPath + @"\" + $"{createPPBindingModel.FileName}", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue); //This closes the created in the background application and quits it. objPresentation.Close(); objPowerPoint.Quit(); }
public string getShapeText(Microsoft.Office.Interop.PowerPoint.Shape shape) { string presentation_text = ""; string textString = ""; if (shape.HasTextFrame == MsoTriState.msoTrue) { if (shape.TextFrame.HasText == MsoTriState.msoTrue) { var textFrame = shape.TextFrame; var textRange = textFrame.TextRange; var paragraphs = textRange.Paragraphs(-1, -1); foreach (Microsoft.Office.Interop.PowerPoint.TextRange paragraph in paragraphs) { var text = paragraph.Text; text = text.Replace("\r", ""); text = text.Replace("\n", " "); presentation_text += text + " "; textString += text + " "; } } } /* * if (shape.HasTable == MsoTriState.msoTrue) * { * var t = shape.Table; * * for (int j = 1; j <= t.Rows.Count; ++j) * for (int k = 1; k <= t.Columns.Count; ++k) * { * //if (shape.HasTextFrame == MsoTriState.msoTrue) * //{ * // if (shape.TextFrame.HasText == MsoTriState.msoTrue) * // { * * var textFrame = t.Cell(j, k).Shape.TextFrame; * var textRange = textFrame.TextRange; * * presentation_text += textRange.Text + " "; * textString += textRange.Text + " "; * * // } * //} * } * } */ //if (shape.HasChart == MsoTriState.msoTrue) //{ // Console.WriteLine("Has Chart: True"); // Microsoft.Office.Interop.PowerPoint.Chart t = shape.Chart; // if (t.HasTitle) // { // Console.WriteLine("Has DataTable: True"); // Console.WriteLine("Title:" + t.ChartTitle.Text.ToString()); textString += t.ChartTitle.Text.ToString() + " "; // } // if (t.HasDataTable) // { // Console.WriteLine("Has DataTable: True"); // var p = t.DataTable; // //textString += t.DataTable.ToString(); // } // textString += shape.Chart.ToString(); // Console.WriteLine("Shape.Chart.tostring()" + shape.Chart.ToString()); // Microsoft.Office.Interop.PowerPoint.SeriesCollection tmp = (Microsoft.Office.Interop.PowerPoint.SeriesCollection)t.SeriesCollection(); // Console.WriteLine("Series Count:" + tmp.Count); // /* // for (int j = 1; j <= tmp.Count; ++j) // { // Microsoft.Office.Interop.PowerPoint.Series aSeries = (Microsoft.Office.Interop.PowerPoint.Series)tmp.Item(j); // foreach (object v in (Array)aSeries.XValues) // { // if (v != null) { Console.WriteLine(v.ToString()); textString += v.ToString() +" "; } // } // foreach (object v in (Array)aSeries.Values) // { // if (v != null) { Console.WriteLine(v.ToString()); textString += v.ToString()+ " "; } // } // } // */ // /* // foreach (Microsoft.Office.Interop.PowerPoint.Series aSeries in tmp ) { // foreach (object v in aSeries.XValues) // { // } // foreach (object v in aSeries.Values as Array) // { // } // var p = aSeries.XValues; // ; // try // { // Console.WriteLine(ArrayToStringGeneric(p, " ")); // } // catch (Exception e) { Console.WriteLine(e.ToString()); } // } // */ //} textString = textString.Replace("\r", ""); textString = textString.Replace("\n", " "); return(textString); }
private string readShape(Microsoft.Office.Interop.PowerPoint.Shape shape) { string str = ""; // Just Checking this ! //comment it (below) out //if (shape.Type == MsoShapeType.msoEmbeddedOLEObject) //{ // Console.WriteLine("Excel Table = true"); //} else //{ // Console.WriteLine("Excel Table = false"); //} // extract text if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue) { var textFrame = shape.TextFrame; if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue) { var textRange = textFrame.TextRange; var paragraphs = textRange.Paragraphs(-1, -1); foreach (PowerPoint.TextRange paragraph in paragraphs) { var text = paragraph.Text; text = text.Replace("\r", "").Replace("\v", " ").Replace("\f", " ").Trim(); text = Regex.Replace(text, @"[^\t\r\n\u0020-\u007E]+", string.Empty); text = Regex.Replace(text, @"[ ]{ 2,}", " "); if (text.Length > 2) { str += "* " + text + "\n"; } } } } // read groups -> ungroup and recursively iterate through this function else if (shape.Type == MsoShapeType.msoGroup) { var p = shape.Ungroup(); foreach (Microsoft.Office.Interop.PowerPoint.Shape shp in p) { str += readShape(shp); } } //read tables in ppt else if (shape.HasTable == MsoTriState.msoTrue) { str += "\n"; var t = shape.Table; for (int j = 1; j <= t.Rows.Count; ++j) { // create the table header for the .md format if (j == 2) { str += "| "; for (int k = 1; k <= t.Columns.Count; ++k) { str += "---| "; } str += "\n"; } // for every other row enter |---|---| notation for cells. str += "| "; for (int k = 1; k <= t.Columns.Count; ++k) { var textFrame = t.Cell(j, k).Shape.TextFrame; var textRange = textFrame.TextRange; var paragraphs = textRange.Paragraphs(-1, -1); foreach (PowerPoint.TextRange paragraph in paragraphs) { var text = paragraph.Text; text = text.Replace("\r", "").Replace("\v", " ").Replace("\f", " "); text = Regex.Replace(text, @"[^\t\r\n\u0020-\u007E]+", string.Empty); text = Regex.Replace(text, @"[ ]{ 2,}", string.Empty); str += " " + text; } str += " | "; } str += "\n"; } str += "\n~\n"; } else if (shape.HasChart == MsoTriState.msoTrue) { Console.WriteLine("Has Chart: True"); Microsoft.Office.Interop.PowerPoint.Chart t = shape.Chart; //string text = ""; if (t.HasTitle) { Console.WriteLine("Has Title: True"); Console.WriteLine("Title:" + t.ChartTitle.Text.ToString()); str += t.ChartTitle.Text.ToString() + " "; } if (t.HasDataTable) { Console.WriteLine("Has DataTable: True"); System.Data.DataTable dp = (System.Data.DataTable)shape.Chart.DataTable; string strRowCommaSeparated = ""; foreach (DataRow dr in dp.Rows) { for (int i = 0; i < dr.ItemArray.Length; i++) { strRowCommaSeparated += strRowCommaSeparated == "" ? dr.ItemArray[i].ToString() : ("," + strRowCommaSeparated); } } Console.WriteLine("\n\n\t\tOur DataTable : " + strRowCommaSeparated); var p = t.DataTable; str += t.DataTable.ToString(); //.DataTable.ToString(); } // Extracting series labels and count Microsoft.Office.Interop.PowerPoint.SeriesCollection tmp = (Microsoft.Office.Interop.PowerPoint.SeriesCollection)t.SeriesCollection(); Console.WriteLine("Series Count:" + tmp.Count); t.ApplyDataLabels(); for (int j = 1; j <= tmp.Count; ++j) { Microsoft.Office.Interop.PowerPoint.Series aSeries = tmp.Item(j); Console.WriteLine("Series Name: " + aSeries.Name); //Microsoft.Office.Interop.PowerPoint.Points pts = tmp.Item(j).Points(); // # point in the chart per series - meaning X axis for a pareto chart //Console.WriteLine("Points #:" + pts.Count); //Console.WriteLine(pts.Item(2).Name); } //Excel Route for chart series data extraction PowerPoint.ChartData pChartData = t.ChartData; //Console.WriteLine("Chart Title:" + t.Title); // No need - shape.title takes care of this if (!t.ChartData.IsLinked) { Console.WriteLine("Has Embedded Excel: True"); Excel.Workbook eWorkbook = (Excel.Workbook)pChartData.Workbook; Excel.Worksheet eWorksheet = (Excel.Worksheet)eWorkbook.Worksheets[1]; var columnsRange = eWorksheet.UsedRange.Columns; var rowsRange = eWorksheet.UsedRange.Rows; var columnCount = columnsRange.Columns.Count; var rowCount = rowsRange.Rows.Count; //Console.WriteLine("r#, c# : " + rowCount + ":" + columnCount); foreach (Excel.Range c in eWorksheet.UsedRange) { string changedCell = c.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); Console.Write(" | " + c.Value2); } eWorkbook.Close(); } else if (t.ChartData.IsLinked) { // add a note for PDF extraction and flagging } //Microsoft.Office.Interop.PowerPoint.SeriesCollection chartSeriesA = (Microsoft.Office.Interop.PowerPoint.SeriesCollection)t.SeriesCollection(); //foreach (Microsoft.Office.Interop.PowerPoint.Series Srs in chartSeriesA) //{ // System.Array a = (System.Array)((object)Srs.Values); // //var XV = Srs.XValues; // //var V = Srs.Values; // //str += Srs.ToString(); //} } //else if (shape.Type == MsoShapeType.msoTextBox || shape.Type == MsoShapeType.msoAutoShape) //{ // var textFrame = shape.TextFrame; // if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue) // { // var textRange = textFrame.TextRange; // var paragraphs = textRange.Paragraphs(-1, -1); // foreach (PowerPoint.TextRange paragraph in paragraphs) // { // var text = paragraph.Text; // text = text.Replace("\r", ""); // str += text + "\n"; // } // } //} //str = CleanInput(str.Replace("\n\n", "\n")); str = Regex.Replace(str, @"[^\t\r\n\u0020-\u007E]+", string.Empty); return(str); //.Replace("\n\n", "\n"); //.Replace("\r",""); }