最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

word ppt excel文檔轉(zhuǎn)換成pdf的C#實現(xiàn)代碼

 更新時間:2014年01月13日 15:46:57   作者:  
這篇文章主要介紹了word ppt excel文檔轉(zhuǎn)換成pdf的C#實現(xiàn)代碼,有需要的朋友可以參考一下

復制代碼 代碼如下:

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轉(zhuǎn)換成功!");
            }
            else
            {
                MessageBox.Show("對不起,轉(zhuǎn)換失敗!");
            }

            if (XLSConvertToPDF("C:/test.xls", "C:/testX.pdf"))
            {
                MessageBox.Show("XLS轉(zhuǎn)換成功!");
            }
            else
            {
                MessageBox.Show("對不起,轉(zhuǎn)換失?。?);
            }
            if (PPTConvertToPDF("C:/需求提綱.pptx", "C:/testP.pdf"))
            {
                MessageBox.Show("PPT轉(zhuǎn)換成功!");
            }
            else
            {
                MessageBox.Show("對不起,轉(zhuǎn)換失?。?);
            }

        }
        //Word轉(zhuǎn)換成pdf
        ///<summary>
        /// 把Word文件轉(zhuǎn)換成為PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路徑</param>
        ///<param name="targetPath">目標文件路徑</param>
        ///<returns>true=轉(zhuǎn)換成功</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文件轉(zhuǎn)換成PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路徑</param>
        ///<param name="targetPath">目標文件路徑</param>
        ///<returns>true=轉(zhuǎn)換成功</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文件轉(zhuǎn)換成PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路徑</param>
        ///<param name="targetPath">目標文件路徑</param>
        ///<returns>true=轉(zhuǎn)換成功</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;
        }
    }
}

相關(guān)文章

最新評論

桐庐县| 高阳县| 辽阳市| 绵竹市| 公主岭市| 朔州市| 苗栗县| 偏关县| 山丹县| 灵武市| 密云县| 依兰县| 萨迦县| 延边| 保亭| 武鸣县| 张家港市| 兴化市| 石柱| 合川市| 漳浦县| 东阿县| 延津县| 惠东县| 阳朔县| 荆门市| 汤阴县| 鄂托克前旗| 永春县| 江西省| 内丘县| 珠海市| 西青区| 常德市| 杭州市| 信宜市| 方正县| 冷水江市| 阿拉善右旗| 成都市| 麻江县|