public async Task <bool> WriteDataFor100(String strID) { try { JObject jsonData = await RequestData.GetWxPPTData("164"); if (jsonData.Value <String>("code").Equals("200") && jsonData.Value <JObject>("data") != null) { JObject dataObj = jsonData["data"].ToObject <JObject>(); string pptdata = dataObj.Value <String>("pptdata"); JArray pptdataArr = JsonConvert.DeserializeObject <JArray>(pptdata); foreach (JObject jObject in pptdataArr) { string picUrl = jObject.Value <String>("picurl"); string content = jObject.Value <String>("content"); int index = (int)decimal.Parse(jObject.Value <string>("index")); PowerPoint.Shapes shapes = Globals.ThisAddIn.Application.ActivePresentation.Slides[index].Shapes; PowerPoint.Shape shape = PPTAPI.getShape(shapes, "pic_1"); if (shape != null) { if (!String.IsNullOrEmpty(picUrl)) { string strPath = Request.HttpDownload(picUrl).Result; shapes.AddPicture(strPath, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height); shape.Delete(); } } shape = PPTAPI.getShape(shapes, "text_1"); if (shape != null) { shape.TextFrame.TextRange.Text = jObject.Value <String>("content"); } } } else { return(false); } } catch { } return(true); }
public void SetTableImage(int slideIdx, int row, int col, Image img, int tableShapeIdx) { if (img == null) { return; } Microsoft.Office.Interop.PowerPoint.Shapes shapes = _slides[slideIdx].Shapes; Microsoft.Office.Interop.PowerPoint.Shape shape = shapes[tableShapeIdx]; string tempPath = @"temp.jpg"; if (File.Exists(tempPath)) { File.Delete(tempPath); } img.Save(tempPath, ImageFormat.Jpeg); int margin = 5; Microsoft.Office.Interop.PowerPoint.Shape tableShape = shape.Table.Cell(row, col).Shape; shapes.AddPicture(tempPath, MsoTriState.msoFalse, MsoTriState.msoTrue, tableShape.Left + margin, tableShape.Top + margin, tableShape.Width - margin, tableShape.Height - margin); }
private static Shape AddClipartObject(this Shapes shapes, ClipartObject clipartObject, Shape shapeTemplate) { Shape shape = null; switch (clipartObject.Type) { case ClipartObjectType.Image: { var imageObject = (ImageClipartObject)clipartObject; var fileName = Path.GetTempFileName(); if (imageObject.Image != null) { imageObject.Image.Save(fileName); var originalWidth = imageObject.Image.Width; var originalHeight = imageObject.Image.Height; var percentWidth = shapeTemplate.Width / originalWidth; var percentHeight = shapeTemplate.Height / originalHeight; var percent = new[] { 1, percentWidth, percentHeight }.Min(); var width = (Int32)(originalWidth * percent); var height = (Int32)(originalHeight * percent); shape = shapes.AddPicture(fileName, MsoTriState.msoFalse, MsoTriState.msoCTrue, shapeTemplate.Left + (shapeTemplate.Width - width) / 2, shapeTemplate.Top + (shapeTemplate.Height - height) / 2, width, height); } } break; case ClipartObjectType.Video: { var videoClipartObject = (VideoClipartObject)clipartObject; var originalWidth = videoClipartObject.Thumbnail.Width; var originalHeight = videoClipartObject.Thumbnail.Height; var percentWidth = shapeTemplate.Width / originalWidth; var percentHeight = shapeTemplate.Height / originalHeight; var percent = new[] { 1, percentWidth, percentHeight }.Min(); var width = (Int32)(originalWidth * percent); var height = (Int32)(originalHeight * percent); shape = shapes.AddMediaObject2(videoClipartObject.SourceFilePath, MsoTriState.msoFalse, MsoTriState.msoCTrue, shapeTemplate.Left + (shapeTemplate.Width - width) / 2, shapeTemplate.Top + (shapeTemplate.Height - height) / 2, width, height); var fileName = Path.GetTempFileName(); videoClipartObject.Thumbnail.Save(fileName); shape.MediaFormat.SetDisplayPictureFromFile(fileName); } break; case ClipartObjectType.YouTube: { var youTubeObject = (YouTubeClipartObject)clipartObject; var width = (Int32)shapeTemplate.Width; var height = (Int32)(shapeTemplate.Width * 9 / 16); var youTubeTag = String.Format("<object><embed src='{0}' type='application/x-shockwave-flash'></embed></object>", youTubeObject.EmbeddedUrl); shape = shapes.AddMediaObjectFromEmbedTag(youTubeTag, shapeTemplate.Left + (shapeTemplate.Width - width) / 2, shapeTemplate.Top + (shapeTemplate.Height - height) / 2, width, height); } break; default: throw new ArgumentOutOfRangeException("Undefined clipart type found"); } return(shape); }
public void insertDocument(string relativePath, string nodeRef) { object missingValue = Type.Missing; object trueValue = true; object falseValue = false; try { // Create a new document if no document currently open if (m_PowerPointApplication.ActivePresentation == null) { m_PowerPointApplication.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue); } // WebDAV or CIFS? string strFullPath = m_ServerDetails.getFullPath(relativePath, m_PowerPointApplication.ActivePresentation.FullName, true); string strExtn = Path.GetExtension(relativePath).ToLower(); string fileName = Path.GetFileName(relativePath); string nativeExtn = ".ppt" + (m_SupportsPptx ? "x" : ""); // If we're using WebDAV, then download file locally before inserting if (strFullPath.StartsWith("http")) { // Need to unescape the the original path to get the filename fileName = Path.GetFileName(Uri.UnescapeDataString(relativePath)); string strTempFile = Path.GetTempPath() + fileName; if (File.Exists(strTempFile)) { try { File.Delete(strTempFile); } catch (Exception) { strTempFile = Path.GetTempFileName(); } } WebClient fileReader = new WebClient(); string url = m_ServerDetails.WebClientURL + "download/direct/" + nodeRef.Replace(":/", "") + "/" + Path.GetFileName(relativePath); fileReader.Headers.Add("Cookie: " + webBrowser.Document.Cookie); fileReader.DownloadFile(url, strTempFile); strFullPath = strTempFile; } // Store the active pane to restore it later PowerPoint.Pane activePane = m_PowerPointApplication.ActiveWindow.ActivePane; // Loop through all the panes available looking for the ppViewSlide type foreach (PowerPoint.Pane pane in m_PowerPointApplication.ActiveWindow.Panes) { if (pane.ViewType == PowerPoint.PpViewType.ppViewSlide) { pane.Activate(); break; } } // Now we can get the current slide index PowerPoint.Slide currentSlide = (PowerPoint.Slide)m_PowerPointApplication.ActiveWindow.View.Slide; int currentSlideIndex = currentSlide.SlideIndex; PowerPoint.Shapes shapes = currentSlide.Shapes; // Is another PowerPoint presentation being inserted? if (nativeExtn.IndexOf(strExtn) != -1) { // Load the presentation in a hidden state PowerPoint.Presentation insertPres = m_PowerPointApplication.Presentations.Open( strFullPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); if (insertPres != null) { // Loop through copy-pasting the slides to be inserted int insertIndex = currentSlideIndex + 1; PowerPoint.Slides slides = m_PowerPointApplication.ActivePresentation.Slides; foreach (PowerPoint.Slide insertSlide in insertPres.Slides) { insertSlide.Copy(); slides.Paste(insertIndex++); } // Close the hidden presentation, flagging that we should ignore the close event m_SuppressCloseEvent = true; insertPres.Close(); } } else { // Get default coords for inserting an object object top = m_PowerPointApplication.ActiveWindow.Top; object left = m_PowerPointApplication.ActiveWindow.Left; // Do we have a selection? if (m_PowerPointApplication.ActiveWindow.Selection != null) { if (m_PowerPointApplication.ActiveWindow.Selection.Type == PowerPoint.PpSelectionType.ppSelectionShapes) { // We can refine the insert location top = m_PowerPointApplication.ActiveWindow.Selection.ShapeRange.Top; left = m_PowerPointApplication.ActiveWindow.Selection.ShapeRange.Left; } } if (".bmp .gif .jpg .jpeg .png".IndexOf(strExtn) != -1) { try { // Inserting a bitmap picture PowerPoint.Shape picture = shapes.AddPicture(strFullPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 1, 2, 3, 4); picture.Top = Convert.ToSingle(top); picture.Left = Convert.ToSingle(left); picture.ScaleWidth(1, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft); picture.ScaleHeight(1, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft); } catch (Exception e) { MessageBox.Show(e.Message, Properties.Resources.MessageBoxTitle); } } else { // Inserting a different file type - do it as an OLE object string iconFilename = String.Empty; int iconIndex = 0; string iconLabel = fileName; string defaultIcon = Util.DefaultIcon(strExtn); if (defaultIcon.Contains(",")) { string[] iconData = defaultIcon.Split(new char[] { ',' }); iconFilename = iconData[0]; iconIndex = Convert.ToInt32(iconData[1]); } object filename = strFullPath; float size = 48; shapes.AddOLEObject((float)left, (float)top, size, size, String.Empty, strFullPath, Microsoft.Office.Core.MsoTriState.msoTrue, iconFilename, iconIndex, iconLabel, Microsoft.Office.Core.MsoTriState.msoFalse); } } // Restore the previously-active pane if (activePane != null) { activePane.Activate(); } } catch (Exception e) { MessageBox.Show(Properties.Resources.UnableToInsert + ": " + e.Message, Properties.Resources.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Generates PowerPoint Slide with given Title, Body and selected images /// </summary> public static string generatePowerPointSlide(string titleText, string bodyText, List <ImageToUse> imagesToPPT) { string status = "Success"; string powerPointPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ppSample.pptx"); Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application(); Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation; if (File.Exists(powerPointPath)) { // Append to existing PowerPoint Microsoft.Office.Interop.PowerPoint.Presentations pres = pptApplication.Presentations; pptPresentation = pres.Open(powerPointPath); } else { // Create new PowerPoint pptPresentation = pptApplication.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue); } Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText]; // Acquire slides from presentation Microsoft.Office.Interop.PowerPoint.Slides slides = pptPresentation.Slides; // Append new slide to end of presentation Microsoft.Office.Interop.PowerPoint._Slide slide = slides.AddSlide(slides.Count + 1, customLayout); Microsoft.Office.Interop.PowerPoint.TextRange objText; // Add title from titleTextArea objText = slide.Shapes[1].TextFrame.TextRange; objText.Text = titleText; objText.Font.Name = "Arial"; objText.Font.Size = 32; // Add body from bodyTextArea objText = slide.Shapes[2].TextFrame.TextRange; objText.Text = bodyText; objText.Font.Name = "Arial"; objText.Font.Size = 20; Microsoft.Office.Interop.PowerPoint.Shapes shapes = slide.Shapes; string imagePath = System.IO.Path.Combine(Environment.CurrentDirectory, "image.jpg"); int imageX = 50; // Go through all images, if image was highlighted in grid, post it inside PPT foreach (ImageToUse imageToUse in imagesToPPT) { if (imageToUse.use) { // Save all images being used var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imageToUse.bitmapImage)); using (FileStream stream = new FileStream(imagePath, FileMode.Create)) encoder.Save(stream); // Insert all images into PPT shapes.AddPicture(imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, imageX, 350, 100, 100); imageX += 100; // Delete all images when done with them File.Delete(imagePath); } } try { // Save Power Point pptPresentation.SaveAs(powerPointPath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue); } catch (Exception exception) { status = "Problem while trying to save PowerPoint:\n" + exception.ToString(); } return(status); // pptPresentation.Close(); // pptApplication.Quit(); }
public async Task <bool> WriteData(String strID) { PowerPoint.Shapes shapes = Globals.ThisAddIn.Application.ActivePresentation.Slides[1].Shapes; try { JObject jsonData = await RequestData.GetPredictData(strID); JObject dataObj = jsonData["data"].ToObject <JObject>(); PowerPoint.Shape shape = PPTAPI.getShape(shapes, "Label_1"); if (shape != null) { shape.TextFrame.TextRange.Text = dataObj.Value <String>("labelInfo"); } shape = PPTAPI.getShape(shapes, "DateLabel"); if (shape != null) { String strText = shape.TextFrame.TextRange.Text; strText = strText.Replace("{Date}", dataObj.Value <String>("date")); shape.TextFrame.TextRange.Text = strText; } shape = PPTAPI.getShape(shapes, "Table_1"); if (shape != null) { PowerPoint.Table table = shape.Table; shape = PPTAPI.getShape(shapes, "Chart_1"); PowerPoint.Chart chart = shape.Chart; var ws = chart.ChartData.Workbook.Worksheets[1]; JArray array = dataObj.Value <JArray>("salesData"); List <PredictData> predicts = CoreAPI.Deserialize <List <PredictData> >(array.ToString()); int col = 2; char colStart = 'B'; foreach (PredictData predict in predicts) { int row = 1; String step = colStart + row.ToString(); ws.Range[step].Value = predict.Month; table.Cell(row, col).Shape.TextFrame.TextRange.Text = predict.Sales; row++; step = colStart + row.ToString(); ws.Range[step].Value = predict.Sales; table.Cell(row, col).Shape.TextFrame.TextRange.Text = predict.Ratio; row++; step = colStart + row.ToString(); ws.Range[step].Value = predict.Ratio; col++; colStart++; } } shape = PPTAPI.getShape(shapes, "Image_1"); if (shape != null) { String strUrl = dataObj.Value <String>("imageUrl"); if (!String.IsNullOrEmpty(strUrl)) { string strPath = Request.HttpDownload(strUrl).Result; shapes.AddPicture(strPath, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, shape.Left, shape.Top, shape.Width, shape.Height); shape.Delete(); } } } catch { } return(true); }
public void InsertPhoto(string photoStr, int left, int top, int width, int height) { oShapes.AddPicture(photoStr, MsoTriState.msoTrue, MsoTriState.msoTrue, left, top, width, height); }