当前位置: 移动技术网 > IT编程>开发语言>c# > C#将PPT文件转换成PDF文件

C#将PPT文件转换成PDF文件

2019年07月18日  | 移动技术网IT编程  | 我要评论

这里在提供c#代码,将ppt转成pdf.直接上代码;

要引入microsoft.office.interop.powerpoint; 版本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.powerpoint;
//office 命名空间
namespace officetopdf
{
  //excel 类
  class powerpointconverter
  {
    //构造函数
    public powerpointconverter()
    { }
    /// <summary>
    /// 转换powerpoint 成pdf文档
    /// </summary>
    /// <param name="_lstrinputfile">原文件路径</param>
    /// <param name="_lstroutfile">pdf文件输出路径</param>
    /// <returns>true 成功</returns>
    public bool convertertopdf(string _lstrinputfile, string _lstroutfile)
    {
      microsoft.office.interop.powerpoint.application lobjpowerpointapp = null;
      microsoft.office.interop.powerpoint.presentation lobjppt = null;
      object lobjmissing = system.reflection.missing.value;
      object lobjsavechanges = null;
      try
      {
        lobjpowerpointapp = new microsoft.office.interop.powerpoint.application();
        lobjppt = lobjpowerpointapp.presentations.open(_lstrinputfile, mscore.msotristate.msotrue, mscore.msotristate.msofalse, mscore.msotristate.msofalse);
        lobjppt.saveas(_lstroutfile, ppsaveasfiletype.ppsaveaspdf, mscore.msotristate.msoctrue);       
      }
      catch (exception ex)
      {
        //其他日志操作;
        return false;
      }
      finally
      {
        if (lobjppt != null)
        {
          lobjppt.close();
          marshal.releasecomobject(lobjppt);
          lobjppt = null;
        }
        if (lobjpowerpointapp != null)
        {
          lobjpowerpointapp.quit();
          marshal.releasecomobject(lobjpowerpointapp);
          lobjpowerpointapp = null;
        }
        //主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行!
        gc.collect();
        gc.waitforpendingfinalizers();
      }
      return true;
    }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网