Saturday, September 22, 2012

Club Multiple Powerpoint to Single File in ASP.NET


C#.NET : Merge multiple PPT into one file



 protected void btnClubPPT_Click(object sender, EventArgs e)
        {
            try
            {
                    String strPath = Server.MapPath("Uploads\\");
                    String strConsolidatedPPT = Server.MapPath("SuccessStory\\");
                    string[] files = Directory.GetFiles(strPath, "*.ppt");
                    for (int i = 0; i < files.Length; i++)
                    {
                        CopySlidesFromPPT(files[i].ToString(), strConsolidatedPPT + "Consolidated Success Story.ppt");
                    }
                }
                System.Diagnostics.Process.Start(Server.MapPath("SuccessStory\\") + "Consolidated Success Story.ppt");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                GC.Collect();
            }



 public static bool CopySlidesFromPPT(string sourcefile, string dstfile)
        {
            bool success = false;

            // Create a link to the PowerPoint object model           

            Microsoft.Office.Interop.PowerPoint.Application oPowerPoint = new Microsoft.Office.Interop.PowerPoint.Application ();
            Microsoft.Office.Interop.PowerPoint.Presentations oPres = oPowerPoint.Presentations;
            Microsoft.Office.Interop.PowerPoint.Presentation oPre = null;

            // If the destination presentation exists on disk, load it so
            // that we can append the new slides           

            if (File.Exists(dstfile) == true)
            {
                try
                {
                    // Try and open the destination presentation
                    oPre = oPres.Open(dstfile, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                }
                catch
                {
                    oPre = null;
                }
            }
            else
            {
                // Create a new presentation
                try
                {
                    oPre = oPres.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
                }
                catch
                {
                    oPre = null;
                }
            }

            // Do we have a master presentation ?          

            if (oPre != null)
            {
                // Point to the slides in the master presentation
                Microsoft.Office.Interop.PowerPoint.Slides oSlides = oPre.Slides;
                try
                {
                    try
                    {
                        // Open the source presentation
                        Microsoft.Office.Interop.PowerPoint.Presentation ppps = oPres.Open(sourcefile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
                        try
                        {
                            // Insert the source slides onto the end of the merge presentation
                            oSlides.InsertFromFile(sourcefile, oSlides.Count, 1, ppps.Slides.Count);
                            oPre.SaveAs(dstfile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoFalse);
                            // Save the merged presentation back to disk
                            // oPre.SaveAs(dstfile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoFalse);
                            // Signal success
                            success = true;
                        }
                        finally
                        {
                            // Close the source presentation
                            ppps.Close();
                        }
                    }
                    catch
                    {

                    }

                }
                finally
                {
                    // Ensure the merge presentation is closed
                    oPre.Close();
                    // Release the COM object holding the merged presentation
                    Marshal.ReleaseComObject(oPre);
                    oPre = null;
                    // Release the COM object holding the presentations
                    Marshal.ReleaseComObject(oPres);
                    oPres = null;
                    // Release the COM object holding the powerpoint application
                    Marshal.ReleaseComObject(oPowerPoint);
                    oPowerPoint = null;
                }
            }
            return success;
        }

3 comments:

  1. Hi Manish

    Thank you. I had a similar task in hand and I am happy that your solution worked for me.
    But , I have an issue..
    The design is not retained in the merged file.. The content is all in place but the theme, design is all lost.
    Please help me

    ReplyDelete
  2. Hi,

    I have tried above code, in my case its retains the design after merging files.

    I will revert you once i find more findings.

    ReplyDelete
  3. Hi Manish,

    Thank You. Your Code worked for me. But in my case power point application fails to open the output file until i restart my system.

    Any idea how to resolve this.

    ReplyDelete

Managing Session Integrity in .NET Core Web Applications: A Middleware Approach

 In our journey of developing a .NET Core web application, we encountered a peculiar challenge with our time-tracking feature. The applicati...