word ppt excel文档转换成pdf的C#实现代码

 更新时间:2014年01月13日 15:46:57   作者:  
这篇文章主要介绍了word ppt excel文档转换成pdf的C#实现代码,有需要的朋友可以参考一下

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
 

namespace ConvertToPDF
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (DOCConvertToPDF("C:/test.doc", "C:/testD.pdf"))
            {
                MessageBox.Show("DOC转换成功!");
            }
            else
            {
                MessageBox.Show("对不起,转换失败!");
            }

            if (XLSConvertToPDF("C:/test.xls", "C:/testX.pdf"))
            {
                MessageBox.Show("XLS转换成功!");
            }
            else
            {
                MessageBox.Show("对不起,转换失败!");
            }
            if (PPTConvertToPDF("C:/需求提纲.pptx", "C:/testP.pdf"))
            {
                MessageBox.Show("PPT转换成功!");
            }
            else
            {
                MessageBox.Show("对不起,转换失败!");
            }

        }
        //Word转换成pdf
        ///<summary>
        /// 把Word文件转换成为PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路径</param>
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        private bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
            bool result=false;
            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;
            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;

                Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;

                wordDocument = wordApplication.Documents.Open(
                ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing);

                if (wordDocument != null)
                wordDocument.ExportAsFixedFormat(paramExportFilePath,
                paramExportFormat, paramOpenAfterExport,
                paramExportOptimizeFor, paramExportRange, paramStartPage,
                paramEndPage, paramExportItem, paramIncludeDocProps,
                paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                paramBitmapMissingFonts, paramUseISO19005_1,
                ref paramMissing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        ///<summary>
        /// 把Excel文件转换成PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路径</param>
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        private bool XLSConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            try
            {
                application = new Excel.ApplicationClass();
                object target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);

                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        ///<summary>
        /// 把PowerPoint文件转换成PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路径</param>
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        private bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
    }
}

相关文章

  • C#中的Linq to JSON操作详解

    C#中的Linq to JSON操作详解

    本文详细讲解了C#中的Linq to JSON操作,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Unity实现移动物体到鼠标点击位置

    Unity实现移动物体到鼠标点击位置

    这篇文章主要为大家详细介绍了Unity实现移动物体到鼠标点击位置,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • C# OpenVINO实现图片旋转角度检测

    C# OpenVINO实现图片旋转角度检测

    这篇文章主要为大家详细介绍了C# OpenVINO如何实现图片旋转角度检测,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • C# 8.0新特性介绍

    C# 8.0新特性介绍

    C# 语言是在2000发布的,至今已正式发布了7个版本,每个版本都包含了许多令人兴奋的新特性和功能更新。下面通过本文给大家分享下C# 8.0的三个令人兴奋的新特性,需要的朋友参考下吧
    2017-10-10
  • C#创建Windows服务与服务的安装、卸载

    C#创建Windows服务与服务的安装、卸载

    这篇文章介绍了C#创建Windows服务与服务的安装、卸载,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • C#实现合并及拆分PDF文件的方法

    C#实现合并及拆分PDF文件的方法

    这篇文章主要为大家详细介绍了C#合并及拆分PDF文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C# Split分隔字符串的应用(C#、split、分隔、字符串)

    C# Split分隔字符串的应用(C#、split、分隔、字符串)

    C# Split分隔字符串主要包括用字符串分隔,用多个字符来分隔,用单个字符来分隔等方法实现,下面的具体的实现代码
    2008-11-11
  • C++通过Callback向C#传递数据的方法

    C++通过Callback向C#传递数据的方法

    下面小编就为大家分享一篇C++通过Callback向C#传递数据的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • C#中out保留字用法实例分析

    C#中out保留字用法实例分析

    这篇文章主要介绍了C#中out保留字用法,实例分析了方法返回值时采用out保留字的用法,需要的朋友可以参考下
    2014-09-09
  • C# 从 UTF-8 流中读取字符串的正确方法及代码详解

    C# 从 UTF-8 流中读取字符串的正确方法及代码详解

    在本篇文章里小编给大家整理的是一篇关于C# 从 UTF-8 流中读取字符串的正确方法的知识点内容,有兴趣的朋友们可以学习参考下。
    2021-11-11

最新评论