最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法

 更新時(shí)間:2014年10月23日 10:02:09   投稿:shichen2014  
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法,包含了兩種實(shí)現(xiàn)方法進(jìn)行比對(duì)分析,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法,分享給大家供大家參考。具體實(shí)現(xiàn)步驟如下:

一、添加引用

復(fù)制代碼 代碼如下:
using Microsoft.Office.Interop.Word;

 
二、轉(zhuǎn)換方法
 
1、方法

復(fù)制代碼 代碼如下:
/// <summary>
    /// 把Word文件轉(zhuǎn)換成pdf文件
    /// </summary>
    /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param>
    /// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</param>
    /// <returns>成功返回true,失敗返回false</returns>
    public static bool WordToPdf(string sourcePath, string targetPath)
    {
        bool result = false;
        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//轉(zhuǎn)換格式1.wdExportFormatPDF轉(zhuǎn)換成pdf格式 2.wdExportFormatXPS轉(zhuǎn)換成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;//需要轉(zhuǎn)格式的文件路徑
            string outputFileName = targetPath;//轉(zhuǎn)換完成后PDF或XPS文件的路徑和文件名名稱
            WdExportFormat exportFormat = wdExportFormatPDF;//導(dǎo)出文件所使用的格式
            bool openAfterExport = false;//轉(zhuǎn)換完成后是否打開
            WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//導(dǎo)出方式1.wdExportOptimizeForPrint針對(duì)打印進(jìn)行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對(duì)屏幕顯示進(jìn)行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。
            WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部?jī)?nèi)容(枚舉)
            int from = 0;//起始頁碼
            int to = 0;//結(jié)束頁碼
            WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定導(dǎo)出過程中是否只包含文本或包含文本的標(biāo)記.1.wdExportDocumentContent:導(dǎo)出文件沒有標(biāo)記,2.導(dǎo)出文件有標(biāo)記
            bool includeDocProps = true;//指定是否包含新導(dǎo)出的文件在文檔屬性
            bool keepIRM = true;//
            WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在導(dǎo)出文件中創(chuàng)建書簽,2.wdExportCreateHeadingBookmarks:標(biāo)題和文本框?qū)С龅奈募袆?chuàng)建一個(gè)書簽,3.wdExportCreateWordBookmarks每個(gè)字的書簽,其中包括除包含頁眉和頁腳中的所有書簽導(dǎo)出的文件中創(chuàng)建一個(gè)書簽。
            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、簡(jiǎn)潔方法

復(fù)制代碼 代碼如下:
/// <summary>
    /// 把Word文件轉(zhuǎn)換成pdf文件
    /// </summary>
    /// <param name="sourcePath">需要轉(zhuǎn)換的文件路徑和文件名稱</param>
    /// <param name="targetPath">轉(zhuǎn)換完成后的文件的路徑和文件名名稱</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;
    }

 
三、調(diào)用
復(fù)制代碼 代碼如下:
OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

彭州市| 界首市| 左云县| 涞源县| 屯留县| 红桥区| 镇坪县| 会东县| 乐安县| 襄樊市| 榆树市| 黄陵县| 内江市| 香港 | 海城市| 德清县| 酒泉市| 蕲春县| 临湘市| 循化| 石渠县| 股票| 宜昌市| 宕昌县| 当雄县| 岫岩| 梨树县| 博客| 东城区| 河津市| 长宁区| 婺源县| 石林| 连江县| 安图县| 河西区| 色达县| 淄博市| 湘乡市| 广丰县| 民勤县|