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#实现远程连接ORACLE数据库的方法

    C#实现远程连接ORACLE数据库的方法

    这篇文章主要介绍了C#实现远程连接ORACLE数据库的方法,通过自定义函数db_connection_test实现远程连接Oracle数据库的功能,是非常实用的技巧,需要的朋友可以参考下
    2014-12-12
  • C#异步编程的三种模式

    C#异步编程的三种模式

    本文详细讲解了C#异步编程的三种模式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#使用foreach语句搜索数组元素的方法

    C#使用foreach语句搜索数组元素的方法

    这篇文章主要介绍了C#使用foreach语句搜索数组元素的方法,涉及C#使用foreach语句遍历数组实现搜索功能的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#简单输出日历的方法

    C#简单输出日历的方法

    这篇文章主要介绍了C#简单输出日历的方法,涉及C#针对日期与时间的简单操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • C#/VB.NET实现在PDF文档中插入,替换或删除图片

    C#/VB.NET实现在PDF文档中插入,替换或删除图片

    这篇文章主要为大家详细介绍了如何使用 Spire.PDF for .NET 通过程序在 PDF 文档中插入、替换或删除图片,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • 解析使用enumerator模式简化异步操作的详解

    解析使用enumerator模式简化异步操作的详解

    本篇文章是对使用enumerator模式简化异步操作进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#中datatable去重的方法

    C#中datatable去重的方法

    这篇文章主要介绍了C#中datatable去重的方法,通过两种不同的方法对比分析了datatable去重的技巧,非常具有实用价值,需要的朋友可以参考下
    2014-10-10
  • C# 重写Notification提示窗口的示例代码

    C# 重写Notification提示窗口的示例代码

    本文主要介绍了C# 重写Notification提示窗口的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • C# 如何获取当前进程或线程的ID

    C# 如何获取当前进程或线程的ID

    这篇文章主要介绍了C# 如何获取当前进程或线程的ID,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • C#调用EXE文件实现传参和获取返回结果

    C#调用EXE文件实现传参和获取返回结果

    本文主要介绍了C#调用EXE文件实现传参和获取返回结果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01

最新评论