欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法

    C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將Email地址轉(zhuǎn)成圖片顯示的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#版Windows服務(wù)安裝卸載小工具

    C#版Windows服務(wù)安裝卸載小工具

    這篇文章主要為大家推薦了一款C#版Windows服務(wù)安裝卸載小工具,小巧靈活的控制臺(tái)程序,希望大家喜歡,感興趣的小伙伴們可以參考一下
    2016-07-07
  • C#新手常犯的錯(cuò)誤匯總

    C#新手常犯的錯(cuò)誤匯總

    這篇文章主要介紹了C#新手常犯的錯(cuò)誤匯總,對(duì)于經(jīng)驗(yàn)豐富的C#程序員同樣具有很好的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C# SortedList排序列表的實(shí)現(xiàn)

    C# SortedList排序列表的實(shí)現(xiàn)

    本文主要介紹了C# SortedList排序列表的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C#集合之鏈表的用法

    C#集合之鏈表的用法

    這篇文章介紹了C#集合之鏈表的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法

    C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法

    這篇文章主要介紹了C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法,有需要的朋友可以參考一下
    2014-01-01
  • Unity中協(xié)程IEnumerator的使用方法介紹詳解

    Unity中協(xié)程IEnumerator的使用方法介紹詳解

    本文主要介紹了Unity中協(xié)程IEnumerator的使用方法介紹詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 基于WPF實(shí)現(xiàn)跳動(dòng)的字符效果

    基于WPF實(shí)現(xiàn)跳動(dòng)的字符效果

    這篇文章主要和大家介紹一個(gè)好玩但實(shí)際作用可能不太大的動(dòng)畫(huà)效果:跳動(dòng)的字符,本文將利用WPF實(shí)現(xiàn)這一效果,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-08-08
  • C#把數(shù)字轉(zhuǎn)換成大寫(xiě)金額的代碼實(shí)例

    C#把數(shù)字轉(zhuǎn)換成大寫(xiě)金額的代碼實(shí)例

    這篇文章主要介紹了C#把數(shù)字轉(zhuǎn)換成大寫(xiě)金額的代碼實(shí)例,例如把200轉(zhuǎn)換成“貳佰元”,需要的朋友可以參考下
    2014-05-05
  • C#程序終極調(diào)試實(shí)現(xiàn)windbg的時(shí)間旅行

    C#程序終極調(diào)試實(shí)現(xiàn)windbg的時(shí)間旅行

    這篇文章主要介紹了C#程序終極調(diào)試實(shí)現(xiàn)windbg的時(shí)間旅行示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06

最新評(píng)論