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

时间:2022-10-10 16:51:54 


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;
        }
    }
}

标签:word转换成pdf,ppt转换成pdf,excel转换成pdf
0
投稿

猜你喜欢

  • Java多种经典排序算法(含动态图)

    2023-09-24 00:45:02
  • 详解java封装实现Excel建表读写操作

    2023-12-24 11:17:20
  • C#实现简单的天气预报示例代码

    2022-03-22 22:52:59
  • C#(.net)水印图片的生成完整实例

    2022-05-06 07:05:30
  • Android view绘制流程详解

    2023-05-16 07:53:25
  • redis redisson 集合的使用案例(RList、Rset、RMap)

    2023-11-13 13:50:21
  • SpringBoot 配置文件加密的步骤

    2023-10-23 02:55:55
  • SpringMVC加载控制与Postmand的使用和Rest风格的引入及RestFul开发全面详解

    2021-10-14 16:45:44
  • java数据结构排序算法之树形选择排序详解

    2022-07-22 23:43:17
  • 在Eclipse中运行Solr 基础知识

    2021-07-06 22:51:04
  • Java SpringBoot整合SpringCloud

    2022-11-11 15:07:46
  • 关于MyBaties的基本配置标签总结

    2022-08-09 17:28:44
  • Java接口中尽量避免使用数组

    2022-07-06 23:24:14
  • Eclipse+Java+Swing+Mysql实现工资管理系统

    2023-07-26 08:51:05
  • 详解Java中Duration类的使用方法

    2021-07-30 20:09:28
  • 深入理解Android Bitmap

    2023-07-29 08:52:14
  • Filter、Servlet、Listener的学习_动力节点Java学院整理

    2021-08-12 00:05:49
  • JDK14性能管理工具之jstack使用介绍

    2022-10-11 18:48:01
  • 用Kotlin实现Android点击事件的方法

    2023-01-16 15:56:03
  • java解析XML Node与Element的区别(推荐)

    2021-11-12 18:32:13
  • asp之家 软件编程 m.aspxhome.com