我正在尝试使用powerpoint将.pptx文件无头转换为pdf文件。代码如下。问题是,如果用户因某些其他原因(例如创建幻灯片)打开了powerpoint,并且他们使用调用此代码的功能,那么他们的powerpoint应用程序将关闭。
我该如何避免这种情况?我有其他方法将ppt文件转换为pdf,但它们更慢。理想情况下,我会使用此方法而不是终止正在运行的实例(但在没有运行实例的情况下仍然可以清理)。最坏的情况是,如果powerpoint打开,我会使用较慢的打印方法。我不知道怎么做。
代码:
private void ConvertPowerpointToPdf(string inputFile, string outputFile)
{
var powerPointApp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
Presentation presentation = null;
Presentations presentations = null;
try
{
presentations = powerPointApp.Presentations;
presentation = presentations.Open(inputFile, ReadOnly: MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse, Untitled: MsoTriState.msoFalse);
presentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF,
PpFixedFormatIntent.ppFixedFormatIntentScreen, MsoTriState.msoFalse,
PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PpPrintOutputType.ppPrintOutputSlides,
MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, string.Empty, false, true, true, true, false,
Type.Missing);
}
finally
{
if (presentation != null)
{
presentation.Close();
Release(presentation);
}
Release(presentations);
powerPointApp.Quit();
Release(powerPointApp);
}
}
答案 0 :(得分:0)
事实证明,你可以使用演示计数来检查这一点,如下所示。请注意,您必须小心释放资源。
private void ConvertPowerpointToPdf(string inputFile, string outputFile)
{
var powerPointApp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
Presentation presentation = null;
Presentations presentationList = null;
try
{
presentationList = powerPointApp.Presentations;
presentation = presentationList.Open(inputFile, ReadOnly: MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse, Untitled: MsoTriState.msoFalse);
presentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF,
PpFixedFormatIntent.ppFixedFormatIntentScreen, MsoTriState.msoFalse,
PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PpPrintOutputType.ppPrintOutputSlides,
MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, string.Empty, false, true, true, true, false,
Type.Missing);
}
finally
{
if (presentation != null)
{
presentation.Close();
Release(presentation);
}
bool allowQuit = true;
if (presentationList != null)
{
allowQuit = presentationList.Count == 0;
Release(presentationList);
}
if(allowQuit)
powerPointApp.Quit();
Release(powerPointApp);
}
}