使用第三方插件 Office 实现转换
1.Application方式转换文章来源:https://www.toymoban.com/news/detail-733472.html
/// <summary>
/// Microsoft.Office.Interop.PowerPoint
/// 使用第三方软件 office
/// </summary>
/// <param name="pptPath">需要转换的ppt文件路径</param>
/// <param name="pdfPath">转换后的路径</param>
public static void OfferPptToPdf(string pptPath, string pdfPath)
{
try
{
pdfPath=pdfPath.Replace('/', '\\');
//创建对象 并且打开ppt文件
Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
//if (!File.Exists(newfilePath))//不存在
//{
//ppt文件 保存为pdf文件
pptPresentation.SaveAs(pdfPath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoFalse);
//}
//关闭对象
pptPresentation.Close();
}
catch (Exception ex)
{
throw;
}
}
2.ApplicationClass方式转换文章来源地址https://www.toymoban.com/news/detail-733472.html
/// <summary>
/// Microsoft.Office.Interop.PowerPoint
/// 把PowerPoing文件转换成PDF格式文件
/// </summary>
/// <param name="pptPath">源文件路径</param>
/// <param name="pdfPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool PPTConvertToPDF(string pptPath, string pdfPath)
{
bool result;
pdfPath = pdfPath.Replace('/', '\\');
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
persentation.SaveAs(pdfPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch (Exception ex)
{
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;
}
使用第三方插件 Spire.Presentation 实现转换
/// <summary>
/// Spire.Presentation 正版需要付费
/// </summary>
/// <param name="pptPath"></param>
/// <param name="pdfPath"></param>
public static void pptToPdf(string pptPath, string pdfPath)
{
Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
presentation.LoadFromFile(pptPath);
presentation.SaveToFile(pdfPath, Spire.Presentation.FileFormat.PDF);
}
使用第三方插件 Aspose.Slides 实现转换
public const string Key =
"PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
"UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
"9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
"HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
"dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
"wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
"A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
"m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
"dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
"0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
"djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
"jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
"PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
"3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
"FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
"zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
"aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
"Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
"V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
//去掉水印
//new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
/// <summary>
/// Aspose.Slides 正版需要付费
/// ppt文件转化为pdf文件 AsposepptToPdf
/// </summary>
/// <param name="pptPath"></param>
/// <param name="pdfPath"></param>
/// <returns></returns>
public static bool AsposepptToPdf(string pptPath, string pdfPath)
{
try
{
new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(pptPath);
FileStream fileOS = new FileStream(pdfPath, FileMode.Create);
ppt.Save(fileOS, Aspose.Slides.Export.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
ppt转换帮助类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace TTApi.Methods
{
/// <summary>
/// Aspose.Slides v:15.1.0
/// Spire.Presentation
/// Microsoft.Office.Interop.PowerPoint
/// ppt转换为pdf
/// </summary>
public class PresentationHelper
{
public const string Key =
"PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
"UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
"9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
"HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
"dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
"wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
"A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
"m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
"dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
"0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
"djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
"jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
"PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
"3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
"FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
"zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
"aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
"Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
"V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
//去掉水印
//new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
/// <summary>
/// Aspose.Slides 正版需要付费
/// ppt文件转化为pdf文件 AsposepptToPdf
/// </summary>
/// <param name="pptPath"></param>
/// <param name="pdfPath"></param>
/// <returns></returns>
public static bool AsposepptToPdf(string pptPath, string pdfPath)
{
try
{
new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(pptPath);
FileStream fileOS = new FileStream(pdfPath, FileMode.Create);
ppt.Save(fileOS, Aspose.Slides.Export.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// Spire.Presentation 正版需要付费
/// </summary>
/// <param name="pptPath"></param>
/// <param name="pdfPath"></param>
public static void pptToPdf(string pptPath, string pdfPath)
{
Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
presentation.LoadFromFile(pptPath);
presentation.SaveToFile(pdfPath, Spire.Presentation.FileFormat.PDF);
}
/// <summary>
/// Microsoft.Office.Interop.PowerPoint
/// 使用第三方软件 office
/// </summary>
/// <param name="pptPath">需要转换的ppt文件路径</param>
/// <param name="pdfPath">转换后的路径</param>
public static void OfferPptToPdf(string pptPath, string pdfPath)
{
try
{
pdfPath=pdfPath.Replace('/', '\\');
//创建对象 并且打开ppt文件
Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
//if (!File.Exists(newfilePath))//不存在
//{
//ppt文件 保存为pdf文件
pptPresentation.SaveAs(pdfPath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoFalse);
//}
//关闭对象
pptPresentation.Close();
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// Microsoft.Office.Interop.PowerPoint
/// 把PowerPoing文件转换成PDF格式文件
/// </summary>
/// <param name="pptPath">源文件路径</param>
/// <param name="pdfPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool PPTConvertToPDF(string pptPath, string pdfPath)
{
bool result;
pdfPath = pdfPath.Replace('/', '\\');
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
persentation.SaveAs(pdfPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch (Exception ex)
{
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;
}
}
}
到了这里,关于C# ppt文件转换为pdf文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!