首页 文章

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

提问于
浏览
2

我需要使用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 块中, pptApplication 来了 null ,这意味着应用程序甚至没有打开,有时它说消息过滤器指示应用程序正忙,有时我看到一个 Headers 为"Exporting..."的对话框,其中一个进度条显示导出进度,然后对话框消失,程序永远挂起,直到我强制关闭它 .

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

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

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

UPDATE

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

2 回答

  • 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

    使用interop,您可以尝试使用以下方法生成pdf:

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

    _oPresentation.SaveAs(outputFullPath, 
    
         PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 
    
         Microsoft.Office.Core.MsoTriState.msoCTrue);
    

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

相关问题