asp.net 将ppt,word转化为pdf实现在线浏览详解
1、首先添加应用:COM里面的Micsosoft Office 12.0 Object Library(VS2013基本都有14.0或者15.0 有的话一样的添加,因为我的没有只有12.0)
:
2、添加程序集(扩展)里的引用:记住你前面的Micsosoft Office 12.0 Object Library 版本是多少的就选多少的没有就自己网上下载或者联系我给你,我这里是做例子;

现在可以看到是这样的

3、如果生成解决方案会出问题就点击Microsoft.Office.Interop.Word/PowerPoint点属性把嵌入互操作类型改为false

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

5、写ppt,word转化为pdf的方法(这里特别说明一下sourcePath是需要转换的原文件,targetPath是转换后的路径及名称,所以在转换后的路径里面提前放一个pdf文件让word,ppt转换pdf的时候去替换它)
//// <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, , , 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;
}
6、转化成pdf后复制这里打开pdf文件的代码
/// <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('\\') + );
p.Response.AddHeader("content-disposition", "filename=" + fileName);
p.Response.WriteFile(inFilePath);
p.Response.End();
}
7、调用方法实现预览功能(我实现的是在线预览ppt,word并且第一次打开需要转化pdf,以后打开的文件如果已经转换过了直接调用)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SerialID = GetQueryString("SerialID", "");//获取文件主键
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);
}
}
8、转化过程中如果报错:一般都是调试的时候看得到catch捕捉到异常,我记得是啥啥啥返回错误的。(或者预览出来的pdf是你以前的pdf内容没有转换过)
我的是win7系统打开:控制面板,系统安全,管理工具,然后打开服务,等下还要打开组件

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

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

这样一般的就可以了,Micsosoft Office 12.0 Object Library只有12.0的那么久不要引用上面15.0或者14.0的Word。有需要的我给你 。或者自己上网下载dll文件版本要一样。关于Excel,其他格式的是一样的转换,具体网上有很多方法,我整了好几天终于搞懂,我也是新手遇到了就弄出来,以后用到再来看看。大神勿喷,一起学习不懂的Q我:3011 9459
asp.net 将ppt,word转化为pdf实现在线浏览详解的更多相关文章
- ASP.NET Core的配置(2):配置模型详解
		
在上面一章我们以实例演示的方式介绍了几种读取配置的几种方式,其中涉及到三个重要的对象,它们分别是承载结构化配置信息的Configuration,提供原始配置源数据的ConfigurationProvi ...
 - ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
		
原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...
 - 【译】ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解
		
原文:[译]ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details ...
 - 在ASP.NET 5应用程序中的跨域请求功能详解
		
在ASP.NET 5应用程序中的跨域请求功能详解 浏览器安全阻止了一个网页中向另外一个域提交请求,这个限制叫做同域策咯(same-origin policy),这组织了一个恶意网站从另外一个网站读取敏 ...
 - <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
		
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
 - https://github.com/Lushenggang/show-pdf在线浏览pdf文件在线浏览pdf文件
		
在线浏览pdf文件 https://github.com/Lushenggang/show-pdf https://github.com/Lushenggang/show-pdf
 - vue 中PDF实现在线浏览,禁止下载,打印
		
需求:在线浏览pdf文件,并且禁止掉用户下载打印的效果. 分析:普通的iframe.embed标签都只能实现在线浏览pdf的功能,无法禁止掉工具栏的下载打印功能.只能尝试使用插件,pdfobject. ...
 - 关于将word转化为pdf 文件调用jacob 包
		
用jacob. 先到官方网站上去下载:http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=11836 ...
 - word 转化为PDF 目录没了
		
https://jingyan.baidu.com/article/ca00d56c5d5f8de99febcf54.html 点文件-->导出-->创建PDF-->选项--> ...
 
随机推荐
- Android组件生命周期(二)
			
引言 应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激活状态:对于活动,对用户有时候可见,有时候不可见.组件生 ...
 - php 中 php-fpm 的重启、终止操作命令
			
php-fpm没有启动nginx会报502的错误 php 5.3.3 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|s ...
 - js模块化开发——前端模块化
			
在JavaScript发展初期就是为了实现简单的页面交互逻辑,寥寥数语即可:如今CPU.浏览器性能得到了极大的提升,很多页面逻辑迁移到了客 户端(表单验证等),随着web2.0时代的到来,Ajax技术 ...
 - js原生设计模式——2面向对象编程之继承—new类式继承
			
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
 - 设置与菜单项关联的Activity
			
有些时候,应用程序需要单击某个菜单项时启动其他Activity(包括其他Service).对于这种需求,Android设置不需要开发者编写任何事件处理代码,只要调用MenuItem的setIntent ...
 - PHP扩展之多线程
			
PHP一直以为不支持多线程,后面才知道有基于pThread的扩展包,地址如下: http://php.net/manual/zh/book.pthreads.php 我感兴趣的是以下几个点: 1.Th ...
 - Oak Seeds 网站项目回顾
			
项目是一个教育类网站,先给出网站网址:http://www.oakseeds.cn/ Oak Seeds原本的名字叫做American School,后来更给为Oak Seeds,意为橡木子.网站是为 ...
 - setTimeout小总结
			
▓▓▓▓▓▓ 大致介绍 今天看了一篇文章,觉得写得不错,所以学习了一下,这篇博客是我自己的理解和总结 原文:你应该知道的 setTimeout 秘密 主要内容: 1.setTimeout原理 2.se ...
 - jquery mobile多页面跳转等,data-ajax="false" 问题,
			
当我们的网站引用了jquery mobile的js后,点击页面的链接,你会发现页面无法跳转,因为jquery mobile默认是采用ajax方式来加载网站的,如果你需要跳到另一个页面,需要在a标签加上 ...
 - webapi 发布接口报405错误(angularjs2.0)
			
参考链接:http://www.cnblogs.com/shenbin/p/5680976.html web访问接口报405错误,以前的jQuery访问方式访问接口没有问题. 但是换成angularj ...