a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

word ppt excel文檔轉換成pdf的C#實現代碼

2020-01-24 02:54:46
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九九九九精品九九九九 | 亚洲热在线视频 | 一区二区三区精品视频 | 色婷婷综合久久久 | 国产精品日本一区二区不卡视频 | 久久精品久久久久久久久久久久久 | 国产精品无码专区在线观看 | 99精品国产一区二区三区 | 国产香蕉97碰碰久久人人九色 | 欧美日韩国产一区二区三区 | 国产一区二区影院 | 国产片一区二区三区 | 欧美色图第一页 | 国产精品日本一区二区不卡视频 | 91一区 | 毛片在线视频 | av日韩在线看 | 国产精品98 | 成人黄色在线观看 | 亚洲精品一区二区三区麻豆 | 国产精品99久久久久久宅男 | 欧美专区在线观看 | 九一亚洲精品 | 成人毛片在线观看 | 91精品久久久久久久久 | 1区2区免费视频 | 亚洲日本韩国在线观看 | 精品久久久久久久久久久院品网 | 欧美日韩精品在线 | 亚洲精品电影 | 成人免费在线观看 | 一级h片 | 精品国产乱码久久久久久88av | 日韩激情视频在线观看 | 国产精品视频久久 | 久久亚洲国产视频 | 午夜小视频在线观看 | 亚洲精品一区二区在线观看 | 中文欧美日韩 | 精品一二区 | 黄色网免费看 |