在没有Interop的情况下将powerpoint演示文稿(ppt / x)转换为PDF

时间:2015-09-15 09:26:23

标签: c# pdf powerpoint office-interop

我需要使用C#

将PowerPoint(ppt / pptx)文件转换为PDF

目前,我正在使用此代码:

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应用程序的实例并以编程方式使用它。

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

应用程序随机失败并使用相同的文件:我运行一次,它运行,我再次运行它,它运行,我再次运行它,它不起作用等等...

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

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

更新

我正在运行我的应用程序的PC是安装了Office的Windows 7 PC。我无法安装任何其他内容,这就是我需要找到一个独立库的原因。

2 个答案:

答案 0 :(得分:1)

只有我想到的其他方法是尝试使用libreoffice执行相同的任务。它具有无头模式(无UI),可以从命令行调用,如下所示:

"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。我用它进行了一些转换,并没有遇到任何问题。

答案 1 :(得分:-1)

使用互操作,您可以尝试使用此方法生成pdf:

我已经测试了一次制作50个出口并且工作正常。

 _oPresentation.SaveAs(outputFullPath, 

     PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 

     Microsoft.Office.Core.MsoTriState.msoCTrue);

注意:_oPresentation只是互操作表示实例(Microsoft.Office.Interop.PowerPoint.Presentation)。

相关问题