C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c#将word转换pdf

C#实现Word转为PDF的方法

作者:chenqiangdage

今天小编就为大家分享一篇关于C#实现Word转为PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

这里主要提供一种将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;
    }
  }
}

总结

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

您可能感兴趣的文章:
阅读全文