在不使用Interop的情况下将Powerpoint演示文稿(ppt/x)转换为PDF [英] Converting powerpoint presentations (ppt/x) to PDF without Interop

查看:172
本文介绍了在不使用Interop的情况下将Powerpoint演示文稿(ppt/x)转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#将PowerPoint(ppt/pptx)文件转换为PDF

I need to convert a PowerPoint (ppt/pptx) file to PDF using C#

当前,我正在使用以下代码:

Currently, I'm using this code:

public void PPTXToPDF(string originalPptPath, string pdfPath) {
    // Create COM Objects
    Microsoft.Office.Interop.PowerPoint.Application pptApplication = null;
    Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = null;
    try {
        object unknownType = Type.Missing;

        //start power point
        pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();

        //open powerpoint document
        pptPresentation = pptApplication.Presentations.Open(originalPptPath,
            Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue,
            Microsoft.Office.Core.MsoTriState.msoFalse);

        // save PowerPoint as PDF
        pptPresentation.ExportAsFixedFormat(pdfPath,
            Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF,
            Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,
            MsoTriState.msoFalse, Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
            Microsoft.Office.Interop.PowerPoint.PpPrintOutputType.ppPrintOutputSlides, MsoTriState.msoFalse, null,
            Microsoft.Office.Interop.PowerPoint.PpPrintRangeType.ppPrintAll, string.Empty, true, true, true,
            true, false, unknownType);
    } finally {
        // Close and release the Document object.
        if (pptPresentation != null) {
            pptPresentation.Close();
            pptPresentation = null;
        }

        // Quit PowerPoint and release the ApplicationClass object.
        if(pptApplication != null) {
            pptApplication.Quit();
            pptApplication = null;
        }
    }
}

此代码段使用Interop库,该库创建PowerPoint应用程序的实例并以编程方式使用它.

This snippet uses the Interop libraries, which create an instance of the PowerPoint application and use it programmatically.

问题是该应用程序随机崩溃. 有时在finally块中,pptApplication出现null,这意味着该应用程序甚至没有打开,有时它表示消息过滤器指示该应用程序正忙,有时我看到一个标题为正在导出..."的对话框,其中带有一个显示导出进度的进度条,然后该对话框消失,程序永久挂起,直到我强行关闭它为止.

The problem is that the application randomly crashes. Sometimes in the finally block, pptApplication comes null, which means that the application didn't even open, sometimes it says The message filter indicated that the application is busy, sometimes I see a dialog titled "Exporting..." with a progressbar showing the export progress, then the dialog disappears and the program hangs forever until I force-close it.

应用程序使用相同的文件随机失败:我运行了一次,它可以运行,我再次运行,它可以运行,我再次运行,但是不运行,等等...

The application fails randomly with the same file: I run it once, it works, I run it again, it works, I run it again, it doesn't work, etc...

我无法拥有有时会运行且有时会失败的应用程序,所以我的问题是:是否有可靠的替代方案,可以将PowerPoint文件转换为不使用Interop的PDF?微软是否提供了替代API来执行这些操作而无需打开PowerPoint实例?

I cannot have an application that sometimes works and sometimes fails, so my question is: Is there a reliable alternative for converting PowerPoint files to PDF which doesn't use Interop? Did Microsoft provide an alternative API to do these things without opening an instance of PowerPoint?

目前使用Interop的原因是,如果需要的话,我还必须阅读PowerPoint文件并在幻灯片中搜索形状.

The reason why I'm using Interop at the moment is that I also have to read the PowerPoint file and search for shapes in slides, if that matters.

更新

我正在运行我的应用程序的PC是安装了Office的Windows 7 PC.我无法安装其他任何东西,这就是为什么我需要找到一个独立的库.

The PC I'm running my application is a Windows 7 PC with Office installed. I cannot install anything else, that's why I need to find an independent library.

推荐答案

我想到的另一种方法是尝试对同一任务使用libreoffice.它具有无头模式(无UI),可以从命令行调用,如下所示:

Only other way that comes to my mind is try using libreoffice for the same task. It has headless mode (no UI) and can be called from command line, like this:

"C:\Program Files (x86)\LibreOffice 5\program\soffice.exe" --headless --convert-to pdf:writer_pdf_Export test.pptx

请注意,它会立即退出(因为它是为批量处理而设计的),但是您可以使用FileSystemWatcher监视输出目录,并在其中创建所需文件的时间(以及何时能够获得对其的排他锁)-完毕.您也可以使用它进行批处理,有关更多可用选项,请参见此处- https://help.libreoffice.org /Common/Starting_the_Software_With_Parameters .我用它进行了一些转换,这样做没有问题.

Note that it will exit immeditaly (because it is designed for batched processing), but you can watch output directory using FileSystemWatcher and when desired file was created there (and when you are able to acquire exclusive lock on it) - it is done. You can also do batch processing with it, for more options available look here - https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters. I used it for some conversions and had no problems doing that.

这篇关于在不使用Interop的情况下将Powerpoint演示文稿(ppt/x)转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆