asp.net 将ppt,word转化为pdf实如今线浏览详解

一、首先添加应用:COM里面的Micsosoft Office 12.0 Object Library(VS2013基本都有14.0或者15.0 有的话同样的添加,由于个人没有只有12.0)安全

 

app

 

二、添加程序集(扩展)里的引用:记住你前面的Micsosoft Office 12.0 Object Library 版本是多少的就选多少的没有就本身网上下载或者联系我给你,我这里是作例子;iview

 

 

如今能够看到是这样的工具

 

三、若是生成解决方案会出问题就点击Microsoft.Office.Interop.Word/PowerPoint点属性把嵌入互操做类型改成false学习

 

 

 

四、在程序代码中添加引用:ui

 

 

五、写ppt,word转化为pdf的方法(这里特别说明一下sourcePath是须要转换的原文件,targetPath是转换后的路径及名称,因此在转换后的路径里面提早放一个pdf文件让word,ppt转换pdf的时候去替换它)this

 //// <summary>
        /// 把Word文件转换成pdf文件2
        /// </summary>
        /// <param name="sourcePath">须要转换的文件路径和文件名称</param>
        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool WordToPdf(object sourcePath, string targetPath)
        {
            bool result = false;
            WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                if (document != null)
                {
                    document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close(ref missing, ref missing, ref missing);
                    document = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit(ref missing, ref missing, ref missing);
                    applicationClass = null;
                }
            }
            return result;
        }

        //// <summary>
        /// 把ppt文件转换成pdf文件2
        /// </summary>
        /// <param name="sourcePath">须要转换的文件路径和文件名称</param>
        /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
        /// <returns>成功返回true,失败返回false</returns>
        public static bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType ppSaveAsFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
            object missing = Type.Missing;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                if (persentation != null)
                {
                    persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
            }
            return result;
        }

六、转化成pdf后复制这里打开pdf文件的代码spa

/// <summary>
        /// 打开pdf文件方法
        /// </summary>
        /// <param name="p"></param>
        /// <param name="inFilePath">文件路径及文件名</param>
        public static void Priview(System.Web.UI.Page p, string inFilePath)
        {
            p.Response.ContentType = "Application/pdf";

            string fileName = inFilePath.Substring(inFilePath.LastIndexOf('\\') + 1);
            p.Response.AddHeader("content-disposition", "filename=" + fileName);
            p.Response.WriteFile(inFilePath);
            p.Response.End();
        }

七、调用方法实现预览功能(我实现的是在线预览ppt,word而且第一次打开须要转化pdf,之后打开的文件若是已经转换过了直接调用)调试

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SerialID = GetQueryString("SerialID", "10001");//获取文件主键
                ShowContent();//预览的方法
            }
        }
        public void ShowContent()
        {
            string AttachMent2pdf = Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID));//判断此文件是否之前打开过
            if (File.Exists(AttachMent2pdf)==true)
            {
                Priview(this, Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)));//此文件打开过找到之前的文件直接打开
                return;
            }
            else//若是第一次打开
            {
                if (Directory.Exists(Server.MapPath("DownLoadAttachment")) == false)//判断存放转换后的文件夹是否存在
                {
                    Directory.CreateDirectory(Server.MapPath(@"\DownLoadAttachment\"));//不存在文件夹就建立文件夹
                }
                CopyFile();//复制文件下面有方法(我只想作一次转换后之后打开不转换,因此每次转化的时候都要复制一个fdf文件来准备被替换)
                string _Ext = System.IO.Path.GetExtension(GetPptOrWordStarPath(SerialID));//获取扩展名
                if (_Ext == ".doc" || _Ext == ".docx")//判断是ppt仍是word
                {
                    
                    WordToPdf(GetPptOrWordStarPath(SerialID), Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf",SerialID)));//word转化pdf的方法,上面有特别提醒注意路径
                }
                if (_Ext == ".ppt")
                {
                    PPTConvertToPDF(GetPptOrWordStarPath(SerialID), Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)));
                }
            }
            Priview(this, Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)));
        }
public void CopyFile() { string sourceFile = Server.MapPath(@"\SourceFile\123456.pdf"); string objectFile = Server.MapPath(@"\DownLoadAttachment\" + string.Format("{0}.pdf", SerialID)); if (System.IO.File.Exists(sourceFile)) { System.IO.File.Copy(sourceFile, objectFile, true); } }

八、转化过程当中若是报错:通常都是调试的时候看获得catch捕捉到异常,我记得是啥啥啥返回错误的。(或者预览出来的pdf是你之前的pdf内容没有转换过)code

个人是win7系统打开:控制面板,系统安全,管理工具,而后打开服务,等下还要打开组件

 

 

而后开启动DTC 就是下面的缩写(用到的多右键属性启动方式改自动)

 

 

打开后继续回到系统工具打开组件服务:点开组件服务,点开个人电脑,点开Distributes Transaction Coordinator,右键本地DTC修改为如图的样子

 

这样通常的就能够了,Micsosoft Office 12.0 Object Library只有12.0的那么久不要引用上面15.0或者14.0的Word。有须要的我给你 。或者本身上网下载dll文件版本要同样。关于Excel,其余格式的是同样的转换,具体网上有不少方法,我整了好几天终于搞懂,我也是新手遇到了就弄出来,之后用到再来看看。大神勿喷,一块儿学习不懂的Q我:3011 9459

相关文章
相关标签/搜索