2
votes

I need to convert a PowerPoint (ppt/pptx) file to PDF using C#

Currently, I'm using this code:

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;
        }
    }
}

This snippet uses the Interop libraries, which create an instance of the PowerPoint application and use it programmatically.

The problem is that the application randomly crashes. Sometimes in the finally block, pptApplication comes null, which means that the application didn't even open, sometimes it says The message filter indicated that the application is busy, sometimes I see a dialog titled "Exporting..." with a progressbar showing the export progress, then the dialog disappears and the program hangs forever until I force-close it.

The application fails randomly with the same file: I run it once, it works, I run it again, it works, I run it again, it doesn't work, etc...

I cannot have an application that sometimes works and sometimes fails, so my question is: Is there a reliable alternative for converting PowerPoint files to PDF which doesn't use Interop? Did Microsoft provide an alternative API to do these things without opening an instance of PowerPoint?

The reason why I'm using Interop at the moment is that I also have to read the PowerPoint file and search for shapes in slides, if that matters.

UPDATE

The PC I'm running my application is a Windows 7 PC with Office installed. I cannot install anything else, that's why I need to find an independent library.

2
In the above code, I want to set Slide Size while converting PPT file to PDF using below syntax: powerpointDocument.PageSetup.SlideSize = PpSlideSizeType.ppSlideSizeA4Paper; But the moment debugger hits the above line, exception is thrown as "PageSetup (unknown member) : Failed." Any clue what is causing this?Saket

2 Answers

1
votes

Only other way that comes to my mind is try using libreoffice for the same task. It has headless mode (no UI) and can be called from command line, like this:

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

Note that it will exit immeditaly (because it is designed for batched processing), but you can watch output directory using FileSystemWatcher and when desired file was created there (and when you are able to acquire exclusive lock on it) - it is done. You can also do batch processing with it, for more options available look here - https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters. I used it for some conversions and had no problems doing that.

-1
votes

Using interop you can try to generate the pdf using this approach:

I have tested making fifty exports at one time and worked fine.

 _oPresentation.SaveAs(outputFullPath, 

     PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 

     Microsoft.Office.Core.MsoTriState.msoCTrue);

Note: _oPresentation is just the interop presentation instance (Microsoft.Office.Interop.PowerPoint.Presentation).