在 .net 中以编程方式将 Power Point 演示文稿转换为 PDF

Programmatically convert Power Point presentation to PDF in .net

经过一番搜索,我仍然没有得到我需要的东西。我正在尝试使用 VB.Net 将各种文件转换为 PDF 并将各种 MS Office 组件(如 Word/Excel/PowerPoint 引用为 COM 对象或使用 PIA(Office 互操作程序集)。最后我想使用 COM 方法,因为它与版本无关,这很重要。

对于 Word 和 Excel 我可以让它同时工作。但是对于 PowerPoint,我 运行 遇到了问题,希望能得到一些建议。

这是两种方法

COM

Dim appPP As Object = CreateObject("PowerPoint.Application")
Dim docPP As Object = appPP.Presentations.Open(strAttachmentFileName)
'2 is Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF
docPP.ExportAsFixedFormat(strNewFileName, 2)
docPP.Close()
appPP.Quit()

此方法在导出行出现错误 - 类型不匹配。 (HRESULT 异常:0x80020005 (DISP_E_TYPEMISMATCH))

PIA - 这工作正常

Imports Microsoft.Office.Interop
' PowerPoint requires this, to add a reference use COM - MS Office Type Library of same version as interop
Imports Microsoft.Office.Core 
Dim appPP As New PowerPoint.Application
Dim docPP As PowerPoint.Presentation = appPP.Presentations.Open(strAttachmentFileName)
docPP.ExportAsFixedFormat(strNewFileName, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF)
docPP.Close()
appPP.Quit()

编辑

同样在 COM 版本中,我尝试了完全合格的版本,包括像这样引用 PIA 和 Office Core

docPP.ExportAsFixedFormat(strNewFileName, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentScreen, MsoTriState.msoFalse, PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PowerPoint.PpPrintOutputType.ppPrintOutputSlides, MsoTriState.msoFalse, 无, PowerPoint.PpPrintRangeType.ppPrintAll, "", 假, 假, 假, 假, 假)

它仍然得到错误

在试验了 COM 版本的 ExportAsFixedFormat 之后,我认为 COM 对象中存在错误。我确实找到了一个可行的解决方案

docPP.SaveAs(strNewFileName, 32)

我不确定版本兼容性如何。我有 Office 2010。