这篇文章主要介绍了ASP.NET实现将word文档转换成pdf的方法,包含了两种实现方法进行比对分析,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了ASP.NET实现将word文档转换成pdf的方法,分享给大家供大家参考。具体实现步骤如下:

一、添加引用

复制代码代码如下:
using Microsoft.Office.Interop.Word;

二、转换方法
 
1、方法

复制代码代码如下:
/// <summary>
    /// 把Word文件转换成pdf文件
    /// </summary>
    /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
    /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
    /// <returns>成功返回true,失败返回false</returns>
    public static bool WordToPdf(string sourcePath, string targetPath)
    {
        bool result = false;
        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式
        object missing = Type.Missing;
        Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
        Document document = null;
        try
        {
            applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            object inputfileName = sourcePath;//需要转格式的文件路径
            string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称
            WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式
            bool openAfterExport = false;//转换完成后是否打开
            WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。
            WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)
            int from = 0;//起始页码
            int to = 0;//结束页码
            WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记
            bool includeDocProps = true;//指定是否包含新导出的文件在文档属性
            bool keepIRM = true;//
            WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。
            bool docStructureTags = true;
            bool bitmapMissingFonts = true;
            bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)
            document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            if (document != null)
            {
                document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);
            }
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (document != null)
            {
                document.Close(ref missing, ref missing, ref missing);
                document = null;
            }
            if (applicationClass != null)
            {
                applicationClass.Quit(ref missing, ref missing, ref missing);
                applicationClass = null;
            }
        }
        return result;
    }

2、简洁方法

复制代码代码如下:
/// <summary>
    /// 把Word文件转换成pdf文件
    /// </summary>
    /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
    /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
    /// <returns>成功返回true,失败返回false</returns>
    public static bool WordToPdf(object sourcePath, string targetPath)
    {
        bool result = false;
        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
        object missing = Type.Missing;
        Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
        Document document = null;
        try
        {
            applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            if (document != null)
            {
                document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
            }
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (document != null)
            {
                document.Close(ref missing, ref missing, ref missing);
                document = null;
            }
            if (applicationClass != null)
            {
                applicationClass.Quit(ref missing, ref missing, ref missing);
                applicationClass = null;
            }
        }
        return result;
    }

三、调用

复制代码代码如下:
OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

希望本文所述对大家的asp.net程序设计有所帮助。

ASP.NET实现将word文档转换成pdf的方法相关推荐

  1. WORD文档转换成PDF格式

    由于一个客户的项目中需要将WORD文档转换成PDF格式,实战教程如下: 需求分析:客户的项目以B/S结构为主,提供一个WORD文件在后台自动转换成PDF,经过实际 测试, 如果该篇WORD文档有100 ...

  2. 怎么把word文档转换成PDF?

    Word文件完成编辑之后,想要转发给他人,但是担心在转发过程中出现了格式错乱的情况,将word文档转换成PDF格式再转发就可以避免类似情况了.那么如何将word文档转换成PDF文件? 方法一: 在编辑 ...

  3. java将WORD文档转换成pdf文件

    总结对jacob和Itext学习总结.本文试验的是将WORD转换成PDF文件. 实现思路 一.先将WORD文档转换成HMTL文件格式(参阅我的前一文<JAVA操作WORD文档). 二.用流读取H ...

  4. python学习之word文档转换成pdf文档

    平时办公的时候总有word文档转换pdf的操作,关键wps等转换工具都还是收费的.这里利用python代码把多个word文档转换合并成pdf文档的实现.代码运行需要几个必要库,没有下载安装的小伙伴可以 ...

  5. 安卓手机如何将Word文档转换成PDF

    相信大家大会去打印机去打印文件,我们大部分都是拿着Word文档去打印,但是Word文档 受软件版本的限制,它会出现格式错乱或者排版不正确. 但是PDF文档就不会受软件版本以及电脑字体的影响而发生排版. ...

  6. 通过WPS把Word文档转换成PDF格式

    今天小试了把金山的WPS Office 2009个人版,发现里面有个文档生成PDF文件的功能,感觉挺好的,可以把微软的word文档直接输出成PDF,方便快捷,他们都是专业的文字处理软件,输出的PDF文 ...

  7. 将word文档转换成pdf格式【使用Aspose技术实现:亲测可用】

    提示:Java使用Aspose技术将word文件转换成pdf文件 文章目录 一.介绍 二.下载依赖并引入jar包 三.编写功能 一.介绍 Java语言使用Aspose技术将word转换成pdf文件的功 ...

  8. C# word文档转换成PDF格式文档

    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...

  9. word文档转换成swf格式文件在网页中用flash显示

    word文档转换成swf格式文件在网页中用flash显示 在OA系统中我们常常需要将上传的word文档在网页中阅览,一般上传后的文档用html的形式查看是会导致排版混乱的,这次我介绍在ASP.NET中 ...

  10. 把word文档转换成swf格式

    把word文档转换成swf格式 以前介绍过如何把PPT转换成swf格式,见 方法一 方法二:,也介绍过如何把word转换成pdf格式:点击查看,今天介绍如何把word文档转换成swf文件. 使用swf ...

最新文章

  1. 漫画:7 种编程语言的学习曲线
  2. mysql期末考试试卷_mysql测试题
  3. msfvenom java_Msfvenom命令总结大全
  4. es python demo
  5. php的数据校验,php 数据类型校验函数的简单示例
  6. 《算法竞赛入门经典》第三章 3.4
  7. python程序写诗_python写的简单发送邮件的脚本
  8. 设计模式是什么鬼(模板方法)
  9. 文件夹缩写(文件夹空格问题解决)
  10. DeepFake技术--辅助工具
  11. 4种常用扒站工具(webzip、ha_TeleportPro、Offline Explorer、wget)
  12. Oracle v$SQLAREA
  13. python笔记003
  14. 计算机考试机试题目word文档,计算机考试 word
  15. [HDU 5755] Gambler Bo (高斯消元)
  16. mysql update join 更新_mysql多表join时候update更新数据的方法 | 很文博客
  17. 大兴线各站名均有特殊含义,给大家普及一下!
  18. 高通平台开发系列讲解(AI篇)SNPE工作流程介绍
  19. ZN-1AI工业机器人与智能视觉系统应用实训平台
  20. 由13位数字组成的中国标准书号校验码计算实例

热门文章

  1. わたしたちの田村くん
  2. 【python】给excel加密
  3. 【BZOJ1061】【NOI2008】志愿者招募 费用流神题、单纯形裸题(代码费用流)
  4. 计算机第三套演示文稿yswg.pptx,打开考生文件夹下的演示文稿yswg.pptx,根据考生文件夹下的文件“PPT-素材.docx”,按照下列要求完善 - 赏学吧...
  5. OpenMP 参考(指令详解)
  6. 快手,抖音,美拍打造个人IP精准引流!
  7. LeetCode 第35题 搜索插入位置 做题记录
  8. vmware linux虚拟机中添加硬盘
  9. oliver什么意思java_英语名字“oliver”是什么意思?
  10. 为什么趁年轻就应该去创业?