Пример #1
0
        public List <IFileItem> AppendSlides(List <PPT.Slide> srcSlides)
        {
            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;

                #region Alt. slower approach by copy ppt and insert slides from file
                //Copy
                //string tempFile = @"c:\WRK\Temp.pptx";
                //ppt.ActivePresentation.SaveCopyAs(tempFile, PPT.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue);
                //PPT.Presentation tempPPT = ppt.Presentations.Open(tempFile,MsoTriState.msoFalse,MsoTriState.msoFalse, MsoTriState.msoFalse);

                //List<string> slideNames = slides.Select(slide => slide.Name).ToList();
                //var v = from PPT.Slide sl in tempPPT.Slides where !slideNames.Contains(sl.Name) select sl;
                //foreach(PPT.Slide s in v)
                //{
                //    s.Delete();
                //}
                //tempPPT.Save();
                //tempPPT.Close();
                //gallery.Slides.InsertFromFile(tempFile, gallery.Slides.Count);
                #endregion

                PPT.Slides gSlides    = pptPresentation.Slides;
                int        startIndex = gSlides.Count + 1;
                foreach (PPT.Slide slide in srcSlides)
                {
                    slide.Copy();
                    gSlides.Paste();
                }
                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;
            }
        }
Пример #2
0
        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;
        }
Пример #3
0
        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);
            }
        }