码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET将word文档转换成pdf的代码

时间:2014-10-23 09:20:08      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   os   ar   使用   for   strong   sp   

一、添加引用

using Microsoft.Office.Interop.Word;

 

二、转换方法

 

1、方法

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

 

2、简洁方法

 

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

 

三、调用

 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

ASP.NET将word文档转换成pdf的代码

标签:style   http   io   os   ar   使用   for   strong   sp   

原文地址:http://www.cnblogs.com/gc2013/p/4044927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!