温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用ASP.NET怎么将word文档转换成pdf

发布时间:2021-02-26 15:07:19 来源:亿速云 阅读:224 作者:戴恩恩 栏目:开发技术

这篇文章主要为大家详细介绍了利用ASP.NET怎么将word文档转换成pdf,文中示例代码介绍的非常详细,具有一定的参考价值,发现的小伙伴们可以参考一下:

ASP.NET 是什么

ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。

一、添加引用

复制代码 代码如下:

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怎么将word文档转换成pdf,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI