利用ASP.NET怎么將word文檔轉(zhuǎn)換成pdf-創(chuàng)新互聯(lián)

這篇文章主要為大家詳細(xì)介紹了利用ASP.NET怎么將word文檔轉(zhuǎn)換成pdf,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:

創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括網(wǎng)站制作、網(wǎng)站建設(shè)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊(duì)10多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。

ASP.NET 是什么

ASP.NET 是開源,跨平臺,高性能,輕量級的 Web 應(yīng)用構(gòu)建框架,常用于通過 HTML、CSS、JavaScript 以及服務(wù)器腳本來構(gòu)建網(wǎng)頁和網(wǎng)站。

一、添加引用

復(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針對打印進(jìn)行導(dǎo)出,質(zhì)量較高,生成的文件大小較大。2.wdExportOptimizeForOnScreen 針對屏幕顯示進(jìn)行導(dǎo)出,質(zhì)量較差,生成的文件大小較小。
            WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//導(dǎo)出全部內(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、簡潔方法

復(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");

以上就是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,小編為大家收集整理的利用ASP.NET怎么將word文檔轉(zhuǎn)換成pdf,如何覺得創(chuàng)新互聯(lián)建站的內(nèi)容還不錯(cuò),歡迎將創(chuàng)新互聯(lián)建站推薦給身邊好友。

本文名稱:利用ASP.NET怎么將word文檔轉(zhuǎn)換成pdf-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article48/djechp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站排名App設(shè)計(jì)、關(guān)鍵詞優(yōu)化、云服務(wù)器外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化