dotNet基于office实现word转pdf

20171228(WordToPdf_byDotNet)

基于Office的实现步骤

主要是利用了 office com 组件中的 Microsoft.Office.Interop.word.dll 动态链接库文件可以通过 c# 代码实现对 word 文件实现另存为 pdf,xml或者其他格式文件一起对word 文件的修改等等。

相关 API 文档可以参考 microsoft 官方 msdn

一、如何 引入 Microsoft.Office.Interop.word.dll 文件

首先本机安装的是 office 2016 版本,找了好久都没有找到 这个动态链接库文件,这一步挺耗费时间的。

具体查找步骤:

  1. 我使用的是 Everything 软件搜索 Microsoft.Office.Interop.word.dll 才找到这个文件
  2. 找到后拷贝到 自己的project 下或者直接引用到项目中

二、 转换代码实现如下

using Word = Microsoft.Office.Interop.Word

string WordFilePath = @"D:\attatch\product\demo.doc";
object paramMissing = Type.Missing;
Word.Application application = new Word Application();
application.Visible = false;
Word.Document document = null;
wordDocument = wordApplication.Documents.Open(WordFilePath, ref paramMissing, true,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);

document.ExportAsFixedFormat(@"D:\attatch\product\demo.pdf",Word.WdExportFormat.wdExportFormatPDF);
// 关闭进程
application.Quit(ref paramMissing, ref paramMissing, ref paramMissing); 

补充

  1. 基于 .net framework 下 word 转换 除了使用office com组件还可以使用 spire.doc,这是收费软件,前三页转换免费的,或者使用 EVOpdf,EVOpdf 中也有word to pdf 插件,不过这是一个收费的应用。

  2. Everything以及 word.dll 文件下载链接

  3. 基于 office com 组件,我们可以实现很多的 关于word,excel的操作

  4. 原理: 我认为后台还是调用了 office 或者 wps 程序,应是 headless 程序,无界面程序。

  5. 其他blog copy的代码


public bool WordToPDF(string sourcePath, string targetPath)
        {
           bool result = false;
           Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
           Document document = null;
           try
           {
              application.Visible = false;
              document = application.Documents.Open(sourcePath);
              document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
              result = true;
           }
           catch (Exception e)
           {
              Console.WriteLine(e.Message);
              result = false;
           }
           finally
           {
              document.Close();
           }
           return result;
        }


//将word文档转换成PDF格式
    private bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
    {
        bool result;
        object paramMissing = Type.Missing;
        Word.ApplicationClass wordApplication = new Word.ApplicationClass();
        Word.Document wordDocument = null;
        try
        {
            object paramSourceDocPath = sourcePath;
            string paramExportFilePath = targetPath;

            Word.WdExportFormat paramExportFormat = exportFormat;
            bool paramOpenAfterExport = false;
            Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Word.WdExportCreateBookmarks paramCreateBookmarks =
                    Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            wordDocument = wordApplication.Documents.Open(
                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);

            if (wordDocument != null)
                wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            result = true;
        }
        finally
        {
            if (wordDocument != null)
            {
                wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                wordDocument = null;
            }
            if (wordApplication != null)
            {
                wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                wordApplication = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    //将excel文档转换成PDF格式
    private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
    {
        bool result;
        object missing = Type.Missing;
        Excel.ApplicationClass application = null;
        Workbook workBook = null;
        try
        {
            application = new Excel.ApplicationClass();
            object target = targetPath;
            object type = targetType;
            workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);

            workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close(true, missing, missing);
                workBook = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    //将ppt文档转换成PDF格式
    private bool Convert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
    {
        bool result;
        object missing = Type.Missing;
        PowerPoint.ApplicationClass application = null;
        Presentation persentation = null;
        try
        {
            application = new PowerPoint.ApplicationClass();
            persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
            persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (persentation != null)
            {
                persentation.Close();
                persentation = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值