asp.net实现将ppt文档转换成pdf的方法
程序员文章站
2024-02-19 20:11:34
本文实例讲述了asp.net实现将ppt文档转换成pdf的方法。分享给大家供大家参考。具体实现方法如下:
一、添加引用
复制代码 代码如下:using microsof...
本文实例讲述了asp.net实现将ppt文档转换成pdf的方法。分享给大家供大家参考。具体实现方法如下:
一、添加引用
复制代码 代码如下:
using microsoft.office.core;
using microsoft.office.interop.powerpoint;
using microsoft.office.interop.powerpoint;
二、转换方法
复制代码 代码如下:
///<summary>
/// 把powerpoint文件转换成pdf格式文件
///</summary>
///<param name="sourcepath">源文件路径</param>
///<param name="targetpath">目标文件路径</param>
///<returns>成功返回true,失败返回false</returns>
public static bool pptconverttopdf(string sourcepath, string targetpath)
{
bool result;
ppsaveasfiletype ppsaveasfiletype = ppsaveasfiletype.ppsaveaspdf;//转换成pdf
object missing = type.missing;
microsoft.office.interop.powerpoint.applicationclass application = null;
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;
}
/// 把powerpoint文件转换成pdf格式文件
///</summary>
///<param name="sourcepath">源文件路径</param>
///<param name="targetpath">目标文件路径</param>
///<returns>成功返回true,失败返回false</returns>
public static bool pptconverttopdf(string sourcepath, string targetpath)
{
bool result;
ppsaveasfiletype ppsaveasfiletype = ppsaveasfiletype.ppsaveaspdf;//转换成pdf
object missing = type.missing;
microsoft.office.interop.powerpoint.applicationclass application = null;
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;
}
三、调用
复制代码 代码如下:
officetopdf.ppttopdf("d:\\12345.pptx", "d:\\12345.pdf");
希望本文所述对大家的asp.net程序设计有所帮助。