Power Point to PDF Conversion using PowerPoint Automation Services

Continuing from my previous article to convert word document to PDF, I would like to show you on how to utilize Power Point Automation Service to convert Power Point document into PDF document.

The requirement is Power Point Automation Service needs to be configured and started. I will not go through with you how to setup Power Point Automation Service, you could refer it here.

Below is the function to convert Power Point to PDF document, it will return file stream.

private SPFileStream ConvertPowerPointToPDF(SPFile file)
{
   string sFileName = file.Name.Trim();
   string sFileNameExtension = sFileName.Substring(sFileName.LastIndexOf('.') + 1);
   SPFileStream result = null;

   try
   {
      // Convert the file to a stream and create an SPFileStream object for the conversion output.
      Stream fStream = file.OpenBinaryStream();
      SPFileStream stream = new SPFileStream(file.Web, 0x1000);

      // Create the presentation conversion request.
      PdfRequest request = new PdfRequest(fStream, sFileNameExtension, stream);

      // Send the request synchronously, passing in a ‘null’ value for the callback parameter and capturing the response in the result object.
      IAsyncResult aSyncResult = request.BeginConvert(SPServiceContext.GetContext(file.Web.Site), null, null);

      // Use the EndConvert method to get the result. 
      request.EndConvert(aSyncResult);

      if (stream != null)
         result = stream;
   }
   catch (Exception ex)
   {
      // Do your error management here.
   }
   return result;
}

The ItemUpdated event, it will look like below. The PDF document will be added into “Documents Public” library with all custom column / metadata updated as well.

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            try
            {
                SPWeb web = properties.Web;
                SPFolder sourceFolder = properties.List.RootFolder;
                SPListItem sourceItem = properties.ListItem;
                SPFile sourceFile = sourceItem.File;
                string sourceFileName = sourceFile.Name.Trim();

                SPFolder destFolder = web.Lists["Documents Public"].RootFolder;
                SPFile destFile = null;
                string destFileName = sourceFileName.Substring(0, sourceFileName.LastIndexOf('.')) + ".pdf";
                bool isOverwrite = true;
                bool isDestApprovalRequired = false;

                // If File Approved
                if (sourceItem.ModerationInformation.Status == SPModerationStatusType.Approved)
                {
                    // Convert the office file
                    if (sourceFileName.EndsWith(".docx") || sourceFileName.EndsWith(".doc"))
                    {
                        // Check out existing PDF file if exists
                        CheckOutFile(web, String.Format("{0}/{1}/{2}", web.Url, destFolder.Url, destFileName));
                        destFile = destFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, properties.Site.UserToken), isOverwrite );
                    }
                    else if (sourceFileName.EndsWith(".pptx") || sourceFileName.EndsWith(".ppt"))
                    {
                        // Check out existing PDF file if exists
                        CheckOutFile(web, String.Format("{0}/{1}/{2}", web.Url, destFolder.Url, destFileName));
                        destFile = destFolder.Files.Add(destFileName, ConvertPowerPointToPDF(sourceFile), isOverwrite);
                    }

                    if (destFile != null)
                    {
                        // Update PDF File metadata
                        foreach (SPField eachPdfField in destFile.Item.Fields)
                        {
                            if (eachPdfField.FromBaseType == false && !eachPdfField.ReadOnlyField)
                            {
                                destFile.Item[eachPdfField.Id] = sourceItem[eachPdfField.Id];
                            }
                        }

                        // Apply Update to PDF File
                        destFile.Item.Update();
                        destFolder.Update();

                        // Check In PDF File
                        if (destFile != null && destFile.CheckOutType != SPFile.SPCheckOutType.None)
                            destFile.CheckIn("");

                        // Approve
                        if(isDestApprovalRequired)
                            destFile.Approve("");

                        destFolder.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                // Do your error management here.
            }
        }

        private void CheckOutFile(SPWeb web, string sFileUrl)
        {
            try
            {
                SPFile currFile = web.GetFile(sFileUrl);
                if (currFile != null && currFile.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    currFile.CheckOut();
                }
            }
            catch (Exception ex)
            {
                // Do your error management here.
            }
        }
Advertisements

Tagged: , , , , , , , , , ,

13 thoughts on “Power Point to PDF Conversion using PowerPoint Automation Services

  1. eddnes 08/23/2013 at 11:11 AM Reply

    Hy , I have installed Power point automation services on my VM and started all related services, but when i try to make ppt convertion, i had an exception: The specified document cannot be converted, i tried on 3 VM and the same error occurs..

    please any help !

    • tjenho 08/23/2013 at 12:24 PM Reply

      Try to create and convert new simple power point document and see whether you face the same error again. Otherwise, you need to look on SharePoint log to get more obvious error. Cheers …

  2. Salah-Eddine Mekhlouf 08/31/2013 at 8:51 PM Reply

    I tried it before, nothing in the LOGS, i think it’s a biug with SharePoint 2013, I tried on 3 VM and always had the same error.

  3. Salah-Eddine Mekhlouf 09/14/2013 at 8:36 AM Reply

    these hotfixes do not apply to Windows server 2012, PPT automation services, Translation services and Word services do not work in SP2013, it’s crazy !!

  4. Minakshi Patil 10/25/2013 at 4:14 AM Reply

    Hi I have installed Power point automation services on my farm. I can see services running from central administration. Below is my code:

    SPFolder myDocs = myWeb.Folders[mySiteURL + “/myPresentationLib”];
    SPFile myFile = myDocs.Files[mySiteURL + “/myPresentationLib/ComapnyBalPresent.pptx”];
    // Create a stream object for the file
    Stream myFileStream = myFile.OpenBinaryStream();
    SPFileStream myStream = new SPFileStream(myWeb, 0x1000);
    // Rrequest conversion to .pdf format.
    // PdfRequest myRequest = new PdfRequest(myFileStream, “.pptx”, myStream);

    PictureRequest req = new PictureRequest(myFileStream, “.pptx”, PictureFormat.Jpg, myStream);
    // Rrequest is sent synchronously, when
    // ‘null’ value is used for the callback parameter.
    // Response is in the result object.
    IAsyncResult result = req.BeginConvert(SPServiceContext.GetContext(mySite), null, null);
    // Use the EndConvert method to get the result.
    req.EndConvert(result);
    // Add the converted file to the document library.
    SPFile myNewPdfFile = myDocs.Files.Add(“ComapnyBalPresent.Jpg”, myStream, true);

    But i am getting “Could not connect to the conversion service.” at ” req.EndConvert(result);” line. Can you please help me.

    Also, I am having one more question Do we require Office App server to be installed to run PowerPoint Automation Service.

    • tjenho 10/25/2013 at 7:28 AM Reply

      I would suggest you could start with creating simple power point file and convert it, who knows there might be problem with your power point file itself.

      Second thing you could try is to convert to other format like (pdf or png).
      you could find powerpoint automation service here:
      http://msdn.microsoft.com/en-us/library/office/fp179894(v=office.15).aspx

      Other than that, I could not help you further cause I can’t duplicate the issue in my environment.
      I saw you’ve asked question in other forum, hopefully someone could help you out with the issue.

      Cheers,
      Tjen

  5. Minakshi Patil 10/25/2013 at 7:53 AM Reply

    Do we require Office App server to be installed to run PowerPoint Automation Service.

    • tjenho 10/25/2013 at 8:30 AM Reply

      Nope, I got it working without Office Web Apps installed and configured.

      • Minakshi Patil 10/25/2013 at 8:51 AM

        Thansk Tjen I just want to confirm my query. Is there any special access / permission needs to be given for the service?

      • tjenho 10/25/2013 at 9:21 AM

        I just assigned Local Farm as Full Control to my Power Point Automation service. The other thing, obviously the user need to have create / update permission to destination library.

        Cheers,
        Tjen

  6. Minakshi Patil 10/29/2013 at 6:51 AM Reply

    Thanks Tjen, getting one more error “AsyncResult operation exceeded timeout of 00:05:00”.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: