private void GetImagesFromWord(string strFileName)
        {
            using (RichEditDocumentServer srv = new RichEditDocumentServer())
            {
                srv.LoadDocument(strFileName);
                DevExpress.XtraRichEdit.API.Native.Document doc = srv.Document;
                DevExpress.XtraRichEdit.API.Native.ParagraphCollection parCol = doc.Paragraphs;
                DocumentImageCollection dic = doc.GetImages(doc.Range);
                if (dic.Count == 0)
                    return;
                //创建文件夹
                int index = strFileName.LastIndexOf('.');
                if (index == -1)
                    return;
                string strDir = strFileName.Substring(0, index);
                if(!Directory.Exists(strDir))
                    Directory.CreateDirectory(strDir);
 
                
                int i = 0;
                foreach (DocumentImage di in dic)
                {
                    i++;
                    string strNewFileName = strDir + "\\";
                    DevExpress.Office.Utils.OfficeImage officeImage = di.Image;
                    string strSuffix = i.ToString()+".";
                    switch (officeImage.RawFormat) { 
                        case DevExpress.Office.Utils.OfficeImageFormat.Bmp:
                            strSuffix += "bmp";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Gif:
                            strSuffix += "gif";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Jpeg:
                            strSuffix += "jpg";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Png:
                            strSuffix += "png";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Icon:
                            strSuffix += "ico";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Emf:
                            strSuffix += "emf";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Wmf:
                            strSuffix += "wmf";
                            break;
                        
 
                        default:
                            strSuffix += "bmp";
                            break;
                    }
                    strNewFileName += strSuffix;
                    File.WriteAllBytes(strNewFileName, officeImage.GetImageBytes(officeImage.RawFormat));
                }
                 
            }
        }

开发工具:VS2015 及以上; Devexpress 15.1 及以上

个人比较喜欢Devexpress RichEditControl 组件:



点赞(79) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部