winform把Office转成PDF文件

 更新时间:2022年06月14日 09:59:07   作者:springsnow  
这篇文章介绍了winform把Office转成PDF文件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

先要把word或ppt转换为pdf; 以pdf的格式展示,防止文件拷贝。

转换方法

1、安装Word、Excel、PowerPoint组件

注意:需安装Microsoft.Office.Interop.Word\Excel\PowerPoint组件。

程序集如下:

2、转换代码

(1)将Word转换为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转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</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文件转换成PDF格式文件

/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</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文件转换成PDF格式文件

///<summary>
/// 把PowerPoint文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>true=转换成功</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;
}

到此这篇关于winform把Office转成PDF文件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#实现将Email地址转成图片显示的方法

    C#实现将Email地址转成图片显示的方法

    这篇文章主要介绍了C#实现将Email地址转成图片显示的方法,涉及C#操作图片的相关技巧,需要的朋友可以参考下
    2015-06-06
  • C#版Windows服务安装卸载小工具

    C#版Windows服务安装卸载小工具

    这篇文章主要为大家推荐了一款C#版Windows服务安装卸载小工具,小巧灵活的控制台程序,希望大家喜欢,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • C#新手常犯的错误汇总

    C#新手常犯的错误汇总

    这篇文章主要介绍了C#新手常犯的错误汇总,对于经验丰富的C#程序员同样具有很好的参考借鉴价值,需要的朋友可以参考下
    2014-08-08
  • C# SortedList排序列表的实现

    C# SortedList排序列表的实现

    本文主要介绍了C# SortedList排序列表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C#集合之链表的用法

    C#集合之链表的用法

    这篇文章介绍了C#集合之链表的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#获取存储过程返回值和输出参数值的方法

    C#获取存储过程返回值和输出参数值的方法

    这篇文章主要介绍了C#获取存储过程返回值和输出参数值的方法,有需要的朋友可以参考一下
    2014-01-01
  • Unity中协程IEnumerator的使用方法介绍详解

    Unity中协程IEnumerator的使用方法介绍详解

    本文主要介绍了Unity中协程IEnumerator的使用方法介绍详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 基于WPF实现跳动的字符效果

    基于WPF实现跳动的字符效果

    这篇文章主要和大家介绍一个好玩但实际作用可能不太大的动画效果:跳动的字符,本文将利用WPF实现这一效果,感兴趣的小伙伴可以学习一下
    2023-08-08
  • C#把数字转换成大写金额的代码实例

    C#把数字转换成大写金额的代码实例

    这篇文章主要介绍了C#把数字转换成大写金额的代码实例,例如把200转换成“贰佰元”,需要的朋友可以参考下
    2014-05-05
  • C#程序终极调试实现windbg的时间旅行

    C#程序终极调试实现windbg的时间旅行

    这篇文章主要介绍了C#程序终极调试实现windbg的时间旅行示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06

最新评论