c# word转tif

预备工作:完整安装office 2003,然后添加COM组件引用,Microsoft WORD 11.0 在打印机和传真机选项中将Microsoft Office Document Image Writer设为默认打印机 需添加的命名空间:using Microsoft.Office.Interop.Word; using System.IO; class PrintDocToTif ...{ string tifPath = null; //设置生成的tif文件路径 //路径名的命名规则为无后缀名,否则调用 varDoc.PrintOut方法时会打开打印预览窗体 public PrintDocToTif(string buildTifPath) ...{ this.tifPath = buildTifPath; } /**//// < summary> /// 生成无后缀名的tif文件 /// < /summary> /// < param name="sourceFile">源WORD文件< /param> /// < param name="destiFile">< /param> public void PrintTo(object sourceFile) ...{ try ...{ //生成的tif路径 object OutputFileName = (object)tifPath; object varMissing = Type.Missing; //定义Word实例对象 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application(); //设置Word的虚拟打印机 varWord.ActivePrinter = "Microsoft Office Document Image Writer"; //定义Word文档实例对象 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open (ref sourceFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); varDoc.Activate(); //激活文档 object PrintToFile = true; //打印成tif文件 varDoc.PrintOut(ref varMissing, ref varMissing, ref varMissing, ref OutputFileName, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); object saveChange = WdSaveOptions.wdSaveChanges; //关闭word文档时不打开新窗体 varDoc.Close(ref saveChange, ref varMissing, ref varMissing); //关闭word文档 varWord.Quit(ref varMissing, ref varMissing, ref varMissing); //退出Word } catch (Exception ex) ...{ throw new Exception(ex.Message); } } /**//// < summary> /// 将无后缀名的生成文件重命名为tif文件 /// < /summary> /// < param name="filePath">生成的无后缀名的文件路径< /param> public void ReNameToTif() ...{ string newTifPath = ""; if (File.Exists(tifPath)) ...{ newTifPath = tifPath + ".tif"; File.Move(tifPath, newTifPath); } } }

你可能感兴趣的:(word)