Logo

C# ppt文件转换为pdf文件

photo

2024年04月09日

使用第三方插件 Office 实现转换

1.Application方式转换



        /// <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方式转换


        /// <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;
        }

    }
}

所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

一.使用背景我们在项目中使用的过程中很多时候需要转pdf,但是有很多控件不行,或者格式不对,所以很...Excel转PDF基于Microsoft.Office.Interop.Excel

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1848 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1822 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,705 作者:Pastore Antonio
1550 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1548 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1534 浏览量