如何使用Microsoft office interop power point

时间:2016-04-01 05:10:38

标签: c# .net powerpoint

这是我试图从PPT文件中提取嵌入图像的代码 使用此代码并发现,此文件中有多少嵌入对象 但是当我尝试从中提取时,我现在从配偶中提取其实际内容

我不使用此代码从PPT文件中提取嵌入图像(.JPEG) 如果有人知道如何提取图像请分享您的代码或链接

谢谢

 using PowerPoint = Microsoft.Office.Interop.PowerPoint;

         private void ultraButtonPPT_Click(object sender, EventArgs e)
        {
            PowerPoint.Application app = new PowerPoint.Application();
            // app.Visible = Office.MsoTriState.msoTrue;
            PowerPoint.Presentation pptPresentation = null;
            //open powerpoint file in your hard drive
            pptPresentation = app.Presentations.Open(@"Presentation2.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
            int j = 1;
            foreach (PowerPoint.Slide slide in pptPresentation.Slides)
            {
                PowerPoint.Shapes slideShapes = slide.Shapes;

                //foreach (PowerPoint.Shape shape in slideShapes)
                //{

                //    var shaptype = shape.Type;
                //    var name = shape.Name;
                //    if (shaptype == Microsoft.Office.Core.MsoShapeType.msoEmbeddedOLEObject)
                //    {

                //        shape.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                //        var fileName = @"C:\Harshil\Projects\EmbededExtraction\EmbededFiles\" + j.ToString("00000") + shape.Name;

                //        string progID = shape.OLEFormat.ProgID;
                //        MemoryStream memStream = null;

                //          shape.Copy();
                //        IDataObject data = Clipboard.GetDataObject();
                //        var abcx = data.GetType();
                //        var ddd = data.GetFormats();
                //        if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
                //        {
                //            Image image = (Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true);
                //            image.Save(fileName + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                //            image.Save(fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                //        }

                //        memStream = (MemoryStream)Clipboard.GetDataObject().GetData("Embedded Object");
                //        if (progID.Equals("Package"))
                //        {
                //            MemoryStream strm = new MemoryStream();
                //       var adf=shape.OLEFormat.ObjectVerbs;
                //            foreach(var xxxx in adf)
                //            {

                //            }

                //            try
                //            {
                //                //If image is valid image then save it as PNG image
                //                Bitmap img = (Bitmap)Bitmap.FromStream(strm);
                //                img.Save(fileName + ".png", ImageFormat.Png);
                //            }
                //            catch
                //            {
                //                Stream file = new FileStream(name + ".object", FileMode.Create);
                //                strm.WriteTo(file);
                //            }
                //            fileName += ".png";
                //        }
                //        if (progID.StartsWith("Word", StringComparison.OrdinalIgnoreCase)) 
                //        {
                //             fileName +=  ".docx";


                //        }

                //        StreamWriter sWriter = new StreamWriter(fileName);
                //        BinaryWriter bWriter = new BinaryWriter(sWriter.BaseStream);
                //        byte[] dataBytes = new byte[memStream.Length];
                //        memStream.Read(dataBytes, 0, (int)memStream.Length);
                //        bWriter.Write(dataBytes);

                //        bWriter.Close();

                //    }
                //    j++;
                //}
                for (int index = 1; index <= slide.Shapes.Count; index++)
                {
                    var shaptype = slide.Shapes[index].Type;
                    var VISIBILITY = slide.Shapes[index].Visible.ToString();
                    if (shaptype == Microsoft.Office.Core.MsoShapeType.msoEmbeddedOLEObject)
                    {
                        if (VISIBILITY.Equals("msoFalse"))
                        {
                            slide.Shapes[index].Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

                        }
                        string progID = slide.Shapes[index].OLEFormat.ProgID;



                        MemoryStream memStream = null;
                        slide.Shapes[index].Copy();
                        memStream = (MemoryStream)Clipboard.GetDataObject().GetData("Embedded Object");
                        string fileSavePath = @"C:\Harshil\Projects\EmbededExtraction\EmbededFiles\" + j.ToString("00000") + slide.Shapes[index].Name;// + ".jpg";
                        if (progID.Equals("Package"))
                        {
                            fileSavePath += ".png";
                        }
                        if (progID.StartsWith("Word", StringComparison.OrdinalIgnoreCase))
                        {
                            fileSavePath += ".docx";


                        }
                        if (progID.StartsWith("paint", StringComparison.OrdinalIgnoreCase))
                        {
                            fileSavePath += ".jpg";
                            IDataObject data = Clipboard.GetDataObject();
                            Image image = (Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true);

                            image.Save(fileSavePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            continue;

                        }

                        StreamWriter sWriter = new StreamWriter(fileSavePath);
                        BinaryWriter bWriter = new BinaryWriter(sWriter.BaseStream);
                        byte[] dataBytes = new byte[memStream.Length];
                        memStream.Read(dataBytes, 0, (int)memStream.Length);
                        bWriter.Write(dataBytes);

                        bWriter.Close();

                    }
                    if (shaptype == Microsoft.Office.Core.MsoShapeType.msoPicture)
                    {
                        //  string progID = slide.Shapes[index].OLEFormat.ProgID;
                        if (VISIBILITY.Equals("msoFalse"))
                        {
                            slide.Shapes[index].Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

                        }
                        var visibale = slide.Shapes[index].Visible;
                        slide.Shapes[index].Copy();
                        System.Drawing.Image image = (System.Drawing.Image)Clipboard.GetDataObject().GetData("Bitmap");

                        // using (var newImage = ScaleImage(image, 300, 400))
                        {
                            string fileSavePath = @"C:\Harshil\Projects\EmbededExtraction\EmbededFiles\" + index + j.ToString("00000") + slide.Shapes[index].Name + ".png";
                            image.Save(fileSavePath);
                        }

                    }
                }
                j++;
            }

            pptPresentation.Close();


        }

0 个答案:

没有答案