麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

ASP.NET實現將word文檔轉換成pdf的方法

2024-07-10 13:28:18
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了ASP.NET實現將word文檔轉換成pdf的方法,包含了兩種實現方法進行比對分析,非常具有實用價值,需要的朋友可以參考下
 
 

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

一、添加引用
 

復制代碼代碼如下:
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程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91九色视频在线播放 | 久久久亚洲欧美综合 | 久久人操| 久久中文一区 | 中文字幕在线网 | 激情久久一区二区 | 成人福利视频在线 | chengrenzaixian| 国产一区不卡 | 色婷婷av一区二区三区久久 | wwwxxx国产| 国产69精品久久久久久久久久 | 成熟女人特级毛片www免费 | 精品一区二区三区免费看 | 久久精品在线免费观看 | 在线成人一区二区 | 精品一区二区免费 | 日本成人在线播放 | 成人国产精品一区二区毛片在线 | 91精品国产乱码久久久久久久久 | 欧美乱论 | 亚洲精品久久久久久 | 国产精品亚洲综合一区二区三区 | 国产精品av久久久久久网址 | 99日韩精品视频 | 久久免费视频一区二区三区 | 国产精品视频不卡 | 黄色免费高清网站 | 久久99精品久久久久久国产越南 | 欧美一级特级 | 视频一区二区三区视频 | 成人毛片免费视频 | 黄色1级视频 | 91美女福利视频 | 国产人成免费爽爽爽视频 | 一级黄色片在线看 | 国产一区二区精彩视频 | 久久伊人精品热在75 | 日韩视频在线一区二区三区 | 禁漫天堂久久久久久久久久 | 亚洲电影免费观看国语版 |