网站首页 博客 C#将word转换成图片
由于需要用到Aspose.Words.dll,所以先添加引用
属性和函数定义
private string current_fullpath = ""; //文件完整路径 private string current_dirname = ""; //文件所在目录 private string current_filename = ""; //文件名(不包含后缀) private string current_fileext = ""; //文件后缀 private int current_totalpage = 0; //转换后的图片数量 private string office2ext = "png"; //转换后的图片保存类型 private string logtext; //日志信息 //将Wrod转换成图片 private void ConvertWord2Image() { Aspose.Words.Document word = null; try { word = new Aspose.Words.Document(this.current_fullpath); } catch (Exception e) { this.logtext = ", " + e.Message + "。"; messagetextBox.AppendText(this.logtext); messagetextBox.ScrollToCaret(); } if (word != null) { this.current_totalpage = word.PageCount; Aspose.Words.Saving.ImageSaveOptions iso = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png); iso.Resolution = 128; iso.PrettyFormat = true; iso.UseAntiAliasing = true; for (int i = 0; i < word.PageCount; i++) { iso.PageIndex = i; int j = i + 1; try { word.Save(this.current_dirname + "\\" + this.current_filename + "_" + j + "." + this.office2ext, iso); } catch (Exception e) { this.logtext = ", " + e.Message + ", 第" + j + "页转换失败。"; messagetextBox.AppendText(this.logtext); messagetextBox.ScrollToCaret(); } } } else { this.logtext = ", 文件无效或者被加密。"; messagetextBox.AppendText(this.logtext); messagetextBox.ScrollToCaret(); } }
调用方法
this.current_fullpath = "E:/file/test2.docx"; //文件完整路径 this.current_fullpath = Path.GetFullPath(this.current_fullpath.Replace("/", "\\\\")); //斜杠替换 this.current_dirname = Path.GetDirectoryName(this.current_fullpath); //文件所在目录 this.current_filename = Path.GetFileNameWithoutExtension(this.current_fullpath); //文件名(不包含后缀) this.current_fileext = "docx"; //文件后缀 this.ConvertWord2Image();