ASP.NET將word文檔轉換成pdf的代碼

來源:互聯網
上載者:User

標籤:io   ar   os   使用   sp   for   on   檔案   bs   

一、添加引用

 

using Microsoft.Office.Interop.Word;

二、轉換方法

1、方法

C# 代碼  

/// <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、簡潔方法

C# 代碼  

/// <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的代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.