Saturday 29 December 2012

Convert PPT to PDF using C#


Convert PPT to PDF using C#

Use this Function to convert PPT  to PDF using C#:


 public static int ConvertPowerPointToPDF()

        {

 string InputFilePath = @"C:\Users\\Desktop\tst1.pps";

            string OutputFilePath = @"C:\Users\\Desktop\tst1.pdf";

            int Errors = 0;

            Microsoft.Office.Interop.PowerPoint._Application PPApplication = null;

            Presentation PPDoc = null;

            try

            {

                // Start an instance of PowerPoint

                PPApplication = new Application();

                //PPApplication.Visible = True

                // Open the source document.

                PPDoc = PPApplication.Presentations.Open(InputFilePath);

                PPDoc.SaveAs(OutputFilePath, PpSaveAsFileType.ppSaveAsPDF);

                //PPDoc.ExportAsFixedFormat(OutputFilePath, PpFixedFormatType.ppFixedFormatTypePDF, PpFixedFormatIntent.ppFixedFormatIntentScreen, Microsoft.Office.Core.MsoTriState.msoCTrue, PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst, PpPrintOutputType.ppPrintOutputBuildSlides, Microsoft.Office.Core.MsoTriState.msoFalse, , , , False, False, False, False, False)

            }

            catch (Exception ex)

            {

                //Interaction.MsgBox(ex.Message);

                //Errors = 1;

            }

            finally

            {

                // Close and release the Document object.

                if ((PPDoc != null))

                {

                    PPDoc.Close();

                    PPDoc = null;

                }

                // Quit PowerPoint and release the ApplicationClass object.

                if ((PPApplication != null))

                {

                    PPApplication.Quit();

                    PPApplication = null;

                }

                GC.Collect();

                GC.WaitForPendingFinalizers();

                GC.Collect();

                GC.WaitForPendingFinalizers();

            }

            return Errors;

        }


For Displaying the PDF in the Page:

Design Part:

<div>
<object type="application/pdf" data="<%= FileName %>" width="1024" height="768">
</div>


Code-Behind:


protected void Page_Load(object sender, EventArgs e)
{
    FileName ="tst1.pdf";
} 

6 comments:

  1. Do I need to add any dll or class files to work above code? Im getting error...

    ReplyDelete
  2. You need to add reference to Microsoft.Office.Interop.PowerPoint.dll

    You can download Microsoft Office Assembly from here:

    http://www.microsoft.com/en-in/download/details.aspx?id=18346

    ReplyDelete
  3. Hi,
    i got following error:

    Retrieving the COM class factory for component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

    I tried with regsvr32 and regasm the interop dll, and also to build for x86...but nothing =(

    ReplyDelete
  4. Refer this link:

    http://forums.asp.net/t/1119052.aspx

    ReplyDelete
  5. Found code to convert ppt or pttx files to pdf format using c# on the link given below:

    http://www.aspose.com/docs/display/slidesnet/Converting%20PPTX%20to%20PDF

    ReplyDelete
  6. Dear Shana, The Code might be correct but these 3rd Party dlls/tools does not come free of cost.. They will definitely cost you a fortune if you want to achieve this functionality in a long run.

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top