C#实现Word转为PDF的方法
程序员文章站
2023-11-27 10:34:34
这里主要提供一种将word文档转成pdf文档的实现;
具体实现看c#代码:要引入microsoft.office.interop.word;版本12.0.0.0...
这里主要提供一种将word文档转成pdf文档的实现;
具体实现看c#代码:要引入microsoft.office.interop.word;版本12.0.0.0
using system; using system.collections.generic; using system.io; using system.linq; using system.text; using system.runtime.interopservices; using microsoft.office.interop.word; //office 命名空间 namespace officetopdf { //excel 类 class wordconverter { //构造函数 public wordconverter() { } /// <summary> /// 转换word 成pdf文档 /// </summary> /// <param name="_lstrinputfile">原文件路径</param> /// <param name="_lstroutfile">pdf文件输出路径</param> /// <returns>true 成功</returns> public bool convertertopdf(string _lstrinputfile, string _lstroutfile) { microsoft.office.interop.word.application lobjwordapp = null; document objdoc = null; object lobjmissing = system.reflection.missing.value; object lobjsavechanges = null; try { object lobjfilename = (object)_lstrinputfile; objdoc = lobjwordapp.documents.open(ref lobjfilename, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing); objdoc.activate(); object lobjoutputfilename = (object)_lstroutfile; object lobjfileformat = wdsaveformat.wdformatpdf; //保存格式为pdf objdoc.saveas(ref lobjoutputfilename,ref lobjfileformat, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing, ref lobjmissing); lobjsavechanges = wdsaveoptions.wddonotsavechanges; ((_document)objdoc).close(ref lobjsavechanges, ref lobjmissing, ref lobjmissing); objdoc = null; ((_application)lobjwordapp).quit(ref lobjsavechanges, ref lobjmissing, ref lobjmissing); lobjwordapp = null; } catch (exception ex) { //其他日志操作; return false; } finally { if (objdoc != null) { ((_document)objdoc).close(ref lobjsavechanges, ref lobjmissing, ref lobjmissing); marshal.releasecomobject(objdoc); objdoc = null; } if (lobjwordapp != null) { ((_application)lobjwordapp).quit(ref lobjsavechanges, ref lobjmissing, ref lobjmissing); marshal.releasecomobject(lobjwordapp); lobjwordapp = null; } //主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行! gc.collect(); gc.waitforpendingfinalizers(); } return true; } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接