• C# word文档转换成PDF格式文档


    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下

        方法:ConvertWordToPdf(string sourcePath, string targetPath)

        sourcePath:word文件路径

         targetPath:生成pdf文件路径

    注:两个路径都为绝对路径

    获取绝对路径:Server.MapPath("../zfjl/wj.docx");

      public static bool ConvertWordToPdf(string sourcePath, string targetPath)
            {
                Microsoft.Office.Interop.Word.WdExportFormat exportFormat;
                exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
                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;
            }
  • 相关阅读:
    目前服务器所需要的技能
    c++11 初始化列表 bind function 示例
    c++11 时间相关操作练习
    C++ Crypto++ RSA加密资料收集
    多线程查找大量数据加锁的速度降低
    c++沉思录 学习笔记 第六章 句柄(引用计数指针雏形?)
    c++沉思录 学习笔记 第五章 代理类
    boost asio 一个聊天的基本框架
    c++11 并发 条件变量 超时等待的代码练习
    centos 6.5 hadoop 2.3 初配置
  • 原文地址:https://www.cnblogs.com/zhangjd/p/8085073.html
Copyright © 2020-2023  润新知