C#实现 word、pdf、ppt 转为图片

您所在的位置:网站首页 ppt每页转成图片的软件 C#实现 word、pdf、ppt 转为图片

C#实现 word、pdf、ppt 转为图片

2023-04-18 13:11| 来源: 网络整理| 查看: 265

      office word文档、pdf文档、powerpoint幻灯片是很是经常使用的文档类型,在现实中常常有需求须要将它们转换成图片 -- 即将word、pdf、ppt文档的每一页转换成一张对应的图片,就像先把这些文档打印出来,而后再扫描成图片同样。因此,相似这种将word、pdf、ppt转换为图片的工具,通常又称之为“电子扫描器”,很高端的名字!html

一.那些场合须要将word、pdf、ppt转换为图片?

     在我了解的状况中,一般有以下三种场景,有将word、pdf、ppt文档转换成图片的需求。网络

1. 防止文档被别人剽窃

      若是直接将word、pdf、ppt文档提供给他人,那么他人就能够很容易的Copy整个文档的内容,而后稍做修改,就成为了他本身的东西。框架

      若是咱们将文档转换为图片以后,再提供给他人,那么,剽窃就不只仅是Copy一下那么容易了。ide

2. 节省纸张

      之前为了更好的作到第1点,都是将文档打印出来给别人看,不少文档看一遍就不用了,因此会浪费不少纸张、浪费墨水、消耗打印机和电力。工具

      在倡导低碳节能的今天,使用电子扫描器的意义就更大了。测试

3. 电子白板课件

      相似在线教学、远程培训这样的系统中,老师使用课件(word、pdf、ppt等类型的文档)是基本的需求,课件与电子白板的结合方案通常是这样的:将课件转换成图片,文档的每一页对应着电子白板的每一页,而获得的图片,正是做为白板页的背景图片。这样,老师就能够在课件图片上进行标注、板书了。咱们前段时间研究word、pdf、ppt文档转图片的技术,就是为了给OMCS的电子白板功能作一个扩展课件类型的Demo示例,让其方便地支持word、pdf、ppt类型的课件。ui

二. 如何转换?

      问一下度娘,能够找到不少不少相似将word转换为图片的文章,可是,真正好用的并很少,筛选是个花时间的过程。在这里,咱们直接把筛选的结果呈现出来,而且将其封装成能够直接复用的Class,为之后有一样须要的人节省时间。this

1. 方案一:使用Office COM组件

      (该方案不支持PDF文档,关于PDF转图片的方法,这里有个很好的汇总,推荐给你们:PDF转换成图片的13种方案)加密

      该方案的要求是用户的电脑上必须安装有微软的Office,咱们能够经过.NET与Office COM组件的互操做(Interop)来操做Office文档。spa

      该方案的原理是这样的:经过COM互操做能够在内存中打开Office文档,而后能够访问文档的每一页,而且支持将任意一页的内容复制到粘贴板(以图的形式),这样,咱们再将粘贴板上的内容保存为图片就搞定了。

      原理很简单,实现代码稍微有点麻烦,以下所示:

private Bitmap[] Scan4Word(string filePath) { //复制目标文件,后续将操做副本 string tmpFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + Path.GetFileName(filePath) + ".tmp"; File.Copy(filePath, tmpFilePath); List bmList = new List(); 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); //删除临时文件 } } COM方案

      上述的实现对于小的word文档很好用,可是,若是word文档很大,有不少页,那么,上述调用就会占用很大的内存。

       若是是这种状况,那么,能够将上面的实现改写一下,没获得一页的图片就将其保存到硬盘,而不用在内存中保存了。

       PPT转为图片也是用一样的COM方式,文末会给出word和ppt转图片的COM实现的class下载。

2. 方案二:使用Aspose组件

       使用Aspose组件的好处是,不须要用户的机器上安装Office,也能够完成咱们想要的功能。这个优点实在是太明显了,因此,这是最推荐的方案。并且,Aspose彻底支持word、ppt、和pdf,甚至excel也没问题。

       咱们在演示如何扩展OMCS电子白板课件类型的示范Demo中,采用的就是Aspose组件,感受很稳定很好用。下面的代码就摘自示范Demo中。

public class Word2ImageConverter : IImageConverter { private bool cancelled = false; public event CbGeneric ProgressChanged; public event CbGeneric ConvertSucceed; public event CbGeneric ConvertFailed; public void Cancel() { if (this.cancelled) { return; } this.cancelled = true; } public void ConvertToImage(string originFilePath, string imageOutputDirPath) { this.cancelled = false; ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, null, 200); } /// /// 将Word文档转换为图片 /// /// Word文件路径 /// 图片输出路径,若是为空,默认值为Word所在路径 /// 从PDF文档的第几页开始转换,若是为0,默认值为1 /// 从PDF文档的第几页开始中止转换,若是为0,默认值为Word总页数 /// 设置所需图片格式,若是为null,默认格式为PNG /// 设置图片的像素,数字越大越清晰,若是为0,默认值为128,建议最大值不要超过1024 private void ConvertToImage(string wordInputPath, string imageOutputDirPath, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution) { try { Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath); if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); } if (imageOutputDirPath.Trim().Length == 0) { imageOutputDirPath = Path.GetDirectoryName(wordInputPath); } if (!Directory.Exists(imageOutputDirPath)) { Directory.CreateDirectory(imageOutputDirPath); } if (startPageNum doc.PageCount || endPageNum endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } if (imageFormat == null) { imageFormat = ImageFormat.Png; } if (resolution


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3