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

winform把Office轉(zhuǎn)成PDF文件

 更新時(shí)間:2022年06月14日 09:59:07   作者:springsnow  
這篇文章介紹了winform把Office轉(zhuǎn)成PDF文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

先要把word或ppt轉(zhuǎn)換為pdf; 以pdf的格式展示,防止文件拷貝。

轉(zhuǎn)換方法

1、安裝Word、Excel、PowerPoint組件

注意:需安裝Microsoft.Office.Interop.Word\Excel\PowerPoint組件。

程序集如下:

2、轉(zhuǎn)換代碼

(1)將Word轉(zhuǎn)換為pdf: 

using Microsoft.Office.Core;
using System;
using System.IO;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bool isSuccess = DOCConvertToPDF(Directory.GetCurrentDirectory() + "\\aa.docx", Directory.GetCurrentDirectory() + "\\aa.pdf");
            if (isSuccess)
            {
                pdfViewer1.LoadFromFile(Directory.GetCurrentDirectory() + "\\aa.pdf");
            }
        }

        /// <summary>
        /// Word轉(zhuǎn)換成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路徑</param>
        /// <param name="targetPath">目標(biāo)文件路徑</param>
        /// <returns>true=轉(zhuǎn)換成功</returns>
        public static bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Word.Application app = new Word.Application();
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            try
            {
                app.Visible = false;
                doc = app.Documents.Open(sourcePath);
                doc.ExportAsFixedFormat(targetPath, Word.WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                throw new ApplicationException(ex.Message);
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref saveChanges, ref missing, ref missing);
                    doc = null;
                }
                if (app != null)
                {
                    app.Quit(ref missing, ref missing, ref missing);
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

    }
}

(2)把Excel文件轉(zhuǎn)換成PDF格式文件

/// <summary>
/// 把Excel文件轉(zhuǎn)換成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路徑</param>
/// <param name="targetPath">目標(biāo)文件路徑</param>
/// <returns>true=轉(zhuǎn)換成功</returns>
public static bool XLSConvertToPDF(string sourcePath, string targetPath)
{
    bool result = false;
    Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
    object missing = Type.Missing;
    Excel.Application app = null;
    Excel.Workbook book = null;
    try
    {
        app = new Excel.Application();
        object target = targetPath;
        object type = targetType;
        book = app.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing);
        book.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
        result = true;
    }
    catch (Exception ex)
    {
        result = false;
        throw new ApplicationException(ex.Message);
    }
    finally
    {
        if (book != null)
        {
            book.Close(true, missing, missing);
            book = null;
        }
        if (app != null)
        {
            app.Quit();
            app = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return result;
}

(3)把PowerPoint文件轉(zhuǎn)換成PDF格式文件

///<summary>
/// 把PowerPoint文件轉(zhuǎn)換成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路徑</param>
///<param name="targetPath">目標(biāo)文件路徑</param>
///<returns>true=轉(zhuǎn)換成功</returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
{
    bool result = false;

    PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
    object missing = Type.Missing;
    PowerPoint.Application app = null;
    PowerPoint.Presentation pres = null;
    try
    {
        app = new PowerPoint.Application();
        pres = app.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
        pres.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
        result = true;
    }
    catch (Exception ex)
    {
        result = false;
        throw new ApplicationException(ex.Message);
    }
    finally
    {
        if (pres != null)
        {
            pres.Close();
            pres = null;
        }
        if (app != null)
        {
            app.Quit();
            app = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return result;
}

到此這篇關(guān)于winform把Office轉(zhuǎn)成PDF文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity UGUI Shadow陰影組件的介紹使用示例

    Unity UGUI Shadow陰影組件的介紹使用示例

    這篇文章主要為大家介紹了Unity UGUI Shadow陰影組件的介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • c#中的virtual方法及應(yīng)用場(chǎng)景分析

    c#中的virtual方法及應(yīng)用場(chǎng)景分析

    在 C# 中,virtual?關(guān)鍵字用于修飾方法、屬性、索引器或事件,這篇文章主要介紹了c#中的virtual方法及應(yīng)用場(chǎng)景分析,需要的朋友可以參考下
    2025-03-03
  • C#圖片添加水印的實(shí)現(xiàn)代碼

    C#圖片添加水印的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C#給圖片添加水印的實(shí)現(xiàn)代碼,不僅可以為圖片加文字水印,還可以判斷是否是圖片文件,感興趣的小伙伴們可以參考一下
    2016-02-02
  • C#中增加SQLite事務(wù)操作支持與使用方法

    C#中增加SQLite事務(wù)操作支持與使用方法

    這篇文章主要介紹了C#中增加SQLite事務(wù)操作支持與使用方法,結(jié)合實(shí)例形式分析了C#中針對(duì)SQLite事務(wù)操作的添加及使用技巧,需要的朋友可以參考下
    2017-07-07
  • c# 幾種常見(jiàn)的加密方法的實(shí)現(xiàn)

    c# 幾種常見(jiàn)的加密方法的實(shí)現(xiàn)

    這篇文章主要介紹了c# 幾種常見(jiàn)的加密方法的實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12
  • C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程

    C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程

    本文主要介紹了C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • C#注釋的一些使用方法淺談

    C#注釋的一些使用方法淺談

    C#注釋的一些使用方法淺談,需要的朋友可以參考一下
    2013-04-04
  • c# 常用框架匯總

    c# 常用框架匯總

    這篇文章主要介紹了c# 常用框架匯總,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C#異步調(diào)用的好處和方法分享

    C#異步調(diào)用的好處和方法分享

    我們要明確,為什么要進(jìn)行異步回調(diào)?眾所周知,普通方法運(yùn)行,是單線程的,如果中途有大型操作(如:讀取大文件,大批量操作數(shù)據(jù)庫(kù),網(wǎng)絡(luò)傳輸?shù)龋?,都?huì)導(dǎo)致方法阻塞,表現(xiàn)在界面上就是,程序卡或者死掉,界面元素不動(dòng)了,不響應(yīng)了
    2012-04-04
  • C#實(shí)現(xiàn)的Windows剪貼板監(jiān)視器功能實(shí)例【附demo源碼下載】

    C#實(shí)現(xiàn)的Windows剪貼板監(jiān)視器功能實(shí)例【附demo源碼下載】

    這篇文章主要介紹了C#實(shí)現(xiàn)的Windows剪貼板監(jiān)視器功能,結(jié)合實(shí)例形式分析了C#實(shí)現(xiàn)剪貼板監(jiān)視功能所涉及的相關(guān)Windows API函數(shù)與使用技巧,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

荣昌县| 泸溪县| 许昌县| 高邮市| 托里县| 黑龙江省| 卫辉市| 江都市| 青河县| 蒙阴县| 肇源县| 司法| 平塘县| 收藏| 保亭| 和政县| 航空| 三明市| 铁岭市| 盐津县| 焉耆| 萨嘎县| 瓮安县| 贵阳市| 萍乡市| 新乡市| 四川省| 策勒县| 乐安县| 常宁市| 长武县| 晋州市| 太原市| 翼城县| 崇仁县| 芜湖县| 高唐县| 屯留县| 丰镇市| 赤峰市| 苗栗市|