Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无需互操作即可将powerpoint演示文稿(ppt/x)转换为PDF_C#_Pdf_Powerpoint_Office Interop - Fatal编程技术网

C# 无需互操作即可将powerpoint演示文稿(ppt/x)转换为PDF

C# 无需互操作即可将powerpoint演示文稿(ppt/x)转换为PDF,c#,pdf,powerpoint,office-interop,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.Power

我需要使用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;
        }
    }
}
此代码段使用互操作库,这些库创建PowerPoint应用程序的实例并以编程方式使用它

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

应用程序在同一个文件中随机失败:我运行一次,它工作,我再次运行,它工作,我再次运行,它不工作,等等

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

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

更新


我运行应用程序的电脑是安装了Office的Windows 7电脑。我不能安装其他任何东西,这就是为什么我需要找到一个独立的库。

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

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

请注意,它将立即退出(因为它是为批处理而设计的),但您可以使用FileSystemWatcher监视输出目录,并且当在那里创建所需的文件时(以及当您能够获取该文件的独占锁时),它就完成了。您也可以使用它进行批处理,有关更多可用选项,请参见此处-。我使用它进行了一些转换,在这方面没有问题。

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

我曾经测试过一次出口50个产品,效果很好

 _oPresentation.SaveAs(outputFullPath, 

     PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 

     Microsoft.Office.Core.MsoTriState.msoCTrue);

注意:_oppresentation只是interop演示文稿实例(Microsoft.Office.interop.PowerPoint.presentation)。

需要安装LibreOffice(或OpenOffice)。我没有安装,也无法安装。我只需要依靠电脑本身,而不用安装新软件。我唯一能做的就是在我的应用程序中嵌入DLL,但它们必须独立工作/与PC上的软件一起工作。这就是为什么我要求提供一个不必与Interop一起工作的库。你没有说你不能安装任何东西:)但是,LibreOffice也有便携式版本(所以不需要安装)-那么我认为您注定要使用interop,除非有我不知道的商业解决方案(它不需要安装,也没有任何依赖性:)请让我知道如何使用此互操作将演示文稿中的特定笔记幻灯片转换为pdf?不确定这是否可以通过互操作/自动化实现技巧或检查如何在上述代码中以编程方式设置此选项,我想在使用以下语法将PPT文件转换为PDF时设置幻灯片大小:powerpointDocument.PageSetup.SlideSize=PpSlideSizeType.ppslideSizeA4纸张;但当调试器点击上面一行时,异常被抛出为“PageSetup(未知成员):Failed”。有什么线索导致这一情况吗?