Пример #1
0
        //private Bitmap[] Scan4PowerPoint(string filePath, ICollection<int> pageIndexCollection)
        //{
        //    List<Bitmap> bmList = new List<Bitmap>();
        //    MSPowerPoint.ApplicationClass pptApplicationClass = new MSPowerPoint.ApplicationClass();

        //    try
        //    {
        //        MSPowerPoint.Presentation presentation = pptApplicationClass.Presentations.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
        //        int totalCount = presentation.Slides.Count;
        //        if (pageIndexCollection != null)
        //        {
        //            totalCount = pageIndexCollection.Count;
        //        }
        //        int index = 0;
        //        foreach (MSPowerPoint.Slide slide in presentation.Slides)
        //        {
        //            if (pageIndexCollection == null || pageIndexCollection.Contains(index))
        //            {
        //                slide.Copy();

        //                System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
        //                if (data.GetDataPresent(DataFormats.MetafilePict))
        //                {
        //                    object obj = data.GetData(DataFormats.MetafilePict);
        //                    Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero);
        //                    //ClipboardMetafileHelper.SaveMetafile(metafile, string.Format("{0}.bmp" ,index));
        //                    Bitmap bm = new Bitmap(metafile);
        //                    bmList.Add(bm);
        //                    Clipboard.Clear();
        //                }
        //            }
        //            ++index;
        //        }

        //        presentation.Close();
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(presentation);

        //        return bmList.ToArray();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        pptApplicationClass.Quit();
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(pptApplicationClass);
        //    }
        //}
        #endregion

        #region Scan4Word
        private Bitmap[] Scan4Word(string filePath)
        {
            //复制目标文件,后续将操作副本
            string tmpFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileName(filePath) + ".tmp";

            File.Copy(filePath, tmpFilePath);

            List <Bitmap> bmList = new List <Bitmap>();

            MSWord.ApplicationClass wordApplicationClass = new MSWord.ApplicationClass();
            wordApplicationClass.Visible = false;
            object missing = System.Reflection.Missing.Value;

            try
            {
                object readOnly       = false;
                object filePathObject = tmpFilePath;

                MSWord.Document document = wordApplicationClass.Documents.Open(ref filePathObject, ref missing,
                                                                               ref readOnly, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing, ref missing, ref missing,
                                                                               ref missing, ref missing);

                bool finished = false;
                while (!finished)
                {
                    document.Content.CopyAsPicture(); //拷贝到粘贴板
                    System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
                    if (data.GetDataPresent(DataFormats.MetafilePict))
                    {
                        object   obj      = data.GetData(DataFormats.MetafilePict);
                        Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero); //从粘贴板获取数据
                        Bitmap   bm       = new Bitmap(metafile.Width, metafile.Height);
                        using (Graphics g = Graphics.FromImage(bm))
                        {
                            g.Clear(Color.White);
                            g.DrawImage(metafile, 0, 0, bm.Width, bm.Height);
                        }
                        bmList.Add(bm);
                        Clipboard.Clear();
                    }

                    object What       = MSWord.WdGoToItem.wdGoToPage;
                    object Which      = MSWord.WdGoToDirection.wdGoToFirst;
                    object startIndex = "1";
                    document.ActiveWindow.Selection.GoTo(ref What, ref Which, ref missing, ref startIndex); // 转到下一页
                    MSWord.Range start = document.ActiveWindow.Selection.Paragraphs[1].Range;
                    MSWord.Range end   = start.GoToNext(MSWord.WdGoToItem.wdGoToPage);
                    finished = (start.Start == end.Start);
                    if (finished) //最后一页
                    {
                        end.Start = document.Content.End;
                    }

                    object oStart = start.Start;
                    object oEnd   = end.Start;
                    document.Range(ref oStart, ref oEnd).Delete(ref missing, ref missing); //处理完一页,就删除一页。
                }

                ((MSWord._Document)document).Close(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(document);

                return(bmList.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                wordApplicationClass.Quit(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplicationClass);
                File.Delete(tmpFilePath); //删除临时文件
            }
        }
Пример #2
0
        public Bitmap[] WordtoImage(string filePath)
        {
            string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileName(filePath) + ".tmp";

            File.Copy(filePath, tmpPath);

            List <Bitmap> imageLst = new List <Bitmap>();

            Microsoft.Office.Interop.Word.Application wordApplicationClass = new Microsoft.Office.Interop.Word.Application();
            wordApplicationClass.Visible = false;
            object missing = System.Reflection.Missing.Value;

            try
            {
                object filePathObject = tmpPath;

                Document document = wordApplicationClass.Documents.Open(ref filePathObject, ref missing,
                                                                        false, ref missing, ref missing, ref missing,
                                                                        ref missing, ref missing, ref missing, ref missing,
                                                                        ref missing, ref missing, ref missing, ref missing,
                                                                        ref missing, ref missing);

                bool finished = false;
                while (!finished)
                {
                    document.Content.CopyAsPicture();
                    System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
                    if (data.GetDataPresent(DataFormats.MetafilePict))
                    {
                        object   obj      = data.GetData(DataFormats.MetafilePict);
                        Metafile metafile = MetafileHelper.GetEnhMetafileOnClipboard(IntPtr.Zero);
                        Bitmap   bm       = new Bitmap(metafile.Width, metafile.Height);
                        using (Graphics g = Graphics.FromImage(bm))
                        {
                            g.Clear(Color.White);
                            g.DrawImage(metafile, 0, 0, bm.Width, bm.Height);
                        }
                        imageLst.Add(bm);
                        Clipboard.Clear();
                    }

                    object Next       = WdGoToItem.wdGoToPage;
                    object First      = WdGoToDirection.wdGoToFirst;
                    object startIndex = "1";
                    document.ActiveWindow.Selection.GoTo(ref Next, ref First, ref missing, ref startIndex);
                    Range start = document.ActiveWindow.Selection.Paragraphs[1].Range;
                    Range end   = start.GoToNext(WdGoToItem.wdGoToPage);
                    finished = (start.Start == end.Start);
                    if (finished)
                    {
                        end.Start = document.Content.End;
                    }

                    object oStart = start.Start;
                    object oEnd   = end.Start;
                    document.Range(ref oStart, ref oEnd).Delete(ref missing, ref missing);
                }

                ((_Document)document).Close(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(document);

                return(imageLst.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                wordApplicationClass.Quit(ref missing, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplicationClass);
                File.Delete(tmpPath);
            }
        }