Converting a Library’s PowerPoint Documents to PDF using PowerPoint Automation Services for SharePoint 2013


  1. Setting up Word Automation Services for SharePoint 2013
  2. Setting up PowerPoint Automation Services for SharePoint 2013
  3. Converting a Library’s Word Documents to PDF
  4. Converting a Library’s PowerPoint documents to PDF
  5. Using ITextSharp to Merge PDF’s into one
  6. Using ITextSharp to create basic PDF page from item property

In my last post, I showed you how to convert a Word document to PDF. In this post I’m going to walk through the code that you can use to convert your PowerPoint document to PDF.

Following on from my last Farm Solution, I have extended the solution by adding another button that when clicked, it looks up the document library and grabs all the PowerPoint documents. Looping through each PowerPoint document it finds, it converts them to PDF and then saves them back to the same library. As before, there is no current way of performing this in Office 365.

The Shared Document library for my site, I have added two PowerPoint documents to my library.

Test 3 is a simple PowerPoint slide that has some text.


And the second PowerPoint document is a “Create an Office Mix” Template created in PowerPoint. As with my word demo, this is just so you can see pictures and layouts converted correctly.


Below is the code and comments explaining what is happening at different stages of the code.

//Will require this using statement
using Microsoft.Office.Server.PowerPoint.Conversion;

protected void btnPowerPointConverttoPDF_Click(object sender, EventArgs e)
{
   //Get current site context
   var site = SPContext.Current.Site;
   var list = web.Lists.TryGetList(libraryName.Text)
   if (list != null)
   {
       PowerPointDocsToConvertToPdf(list);
   }
}

 private void PowerPointDocsToConvertToPdf(SPList library)
 {
    //Perform a SPQuery that returns only PowerPoint Documents.
    SPQuery query = new SPQuery();
    query.Folder = library.RootFolder;

    //Include all subfolders so include Recursive Scope.
    query.ViewXml = @"<View Scope='Recursive'>
       <Query>
            <Where>
                <Or>
                   <Contains>
                     <FieldRef Name='File_x0020_Type'/>
                     <Value Type='Text'>ppt</Value>
                   </Contains>
                   <Contains>
                     <FieldRef Name='File_x0020_Type'/>
                     <Value Type='Text'>pptx</Value>
                   </Contains>
                </Or>
           </Where>
        </Query>
   </View>";

//Get Documents
SPListItemCollection listItems = library.GetItems(query);
//Check that there are any documents to convert.
if (listItems.Count > 0)
{
       foreach (SPListItem li in listItems)
       {
             try
             {
             //Perform the conversion in memory first
             using (MemoryStream destinationStream = new MemoryStream())
             {
                  //Create the conversion request, configure settings.
                 Microsoft.Office.Server.PowerPoint.Conversion.FixedFormatSettings settings = new Microsoft.Office.Server.PowerPoint.Conversion.FixedFormatSettings();
                 settings.BitmapUnembeddableFonts = true;
                 settings.FrameSlides = true;
                 settings.IncludeDocumentProperties = true;
                 settings.IncludeDocumentStructureTags = true;
                 settings.IncludeHiddenSlides = true;
                 settings.OptimizeForMinimumSize = true;
                 settings.UsePdfA = true;
                 settings.UseVerticalOrder = true;

                 //Perform conversion, opening stream, passing in settings and destination stream.
                 PdfRequest request = new PdfRequest(li.File.OpenBinaryStream(), ".pptx", settings, destinationStream);

                 //Send the request synchronusly, passing in null value for the callback parameter and caputring the response in result object.
                 IAsyncResult result = request.BeginConvert(SPServiceContext.GetContext(library.ParentWeb.Site), null, null);

                //Use the EndConvertMethod;
                request.EndConvert(result);

                //Add the converted file to the document library
                var fileName = Path.GetFileNameWithoutExtension(li.File.Name) + ".pdf";
                SPFile newFile = library.RootFolder.Files.Add(fileName, destinationStream, true);
            }
          }
          catch(Exception ex)
          {
            throw new Exception("Error processing PowerPoint to PDF " + ex.message);
          }
     }
  }
}

After typing in the Document Library name and clicking the button, the PowerPoint documents have been converted to PDF.

Link to Sample code : OneDrive

 

Converting a Library’s Word Documents to PDF using Word Automation Services for SharePoint 2013


  1. Setting up Word Automation Services for SharePoint 2013
  2. Setting up PowerPoint Automation Services for SharePoint 2013
  3. Converting a Library’s Word Documents to PDF
  4. Converting a Library’s PowerPoint documents to PDF
  5. Using ITextSharp to Merge PDF’s into one
  6. Using ITextSharp to create basic PDF page from item property

In my previous posts, I showed you how to set up your environment so that you could perform Word or PowerPoint conversion to PDF. In this post I’m going to walk through the code that you can use to convert your word document to PDF.

Back in SharePoint 2010, when you converted a Word Document to PDF you created a Conversion Job, this was to accommodate bulk file operation scenarios. The job was placed in a queue within the Word Automation Services and ran by a SharePoint Timer Job. Due to it being ran on a SharePoint Timer Job, there wasn’t a simple way of determine when a conversion had completed. Developers who wanted to perform a conversion synchronously, and get a response if the file conversion was successful or not just couldn’t. Part of the SharePoint 2013 version of Word Automation Services, you can now call the Word Automation Services on demand. Meaning you can now perform synchronous file conversions. If you wish to perform Asynchronous Word to PDF conversion, there is a MSDN walkthrough article how you might achieve this. https://msdn.microsoft.com/en-us/library/office/ff181518(v=office.14).aspx

I have put together a simple Farm Solution that although it is performing a bulk conversion of Word Documents to PDF, it does it synchronously. There is currently no possible way of doing this process within Office 365, well not without using an on premise farm to perform the actual conversion for you.

I have built a simple visual web part (maybe in a real world scenario this might be a button on the ribbon of the library, or you perform a event receiver on the list that converts any word document to pdf), that has textbox for a user to enter the document library they wish to convert Word Documents to PDF, and a button. The button click calls then looks up the document library, grab all word documents then loops through each word document converting to PDF and then save it back to the same library.

image

The Shared Document library for my site, I have added two word documents to my library.

image

One is a simple test document that says “hello you”.

image

And a second document that is actually an “Event Flyer” Template created in Microsoft Word, and edited slightly. This is just so that you can see pictures and layouts conversion too.

image

Below is the code and comments explaining what is happening at different stages of the code.

//Will require this using statement
using Microsoft.Office.Word.Server.Conversions;

//This is the name of your Word Automation Service within Central Admin. If you have followed my setup yours will be called WAS too.
public static string WORD_AUTOMATION_SERVICE = "WAS";

//Click handler
protected void btnWordConvertToPDF_Click(object sender, EventArgs e)
{
  //Get current site context         
  var web = SPContext.Current.Web;
  var list = web.Lists.TryGetList(libraryName);

 if(list != null)
 {
    WordDocsToConvertToPdf(list);
 }
} 

private void WordDocsToConvertToPdf(SPList library)
{
   //Perform a SPQuery that returns only Word Documents.
   SPQuery query = new SPQuery();
   query.Folder = library.RootFolder;
   //Include all subfolders so include Recursive Scope.
    query.ViewXml = @"<View Scope='Recursive'>
                                <Query>
                                   <Where>
                                        <Or>
                                            <Contains>
                                                <FieldRef Name='File_x0020_Type'/>
                                                <Value Type='Text'>doc</Value>
                                            </Contains>
                                            <Contains>
                                                <FieldRef Name='File_x0020_Type'/>
                                                <Value Type='Text'>docx</Value>
                                            </Contains>
                                        </Or>
                                    </Where>
                                </Query>
                            </View>"; 

  //Get Documents
  SPListItemCollection listItems = library.GetItems(query);

  //Check that there are any documents to convert.
            if (listItems.Count > 0)
            {
                foreach (SPListItem li in listItems)
                {
                    //Perform the conversion in memory first, therefore we require a MemoryStream.
                    using (MemoryStream destinationStream = new MemoryStream())
                    {
                        //Call the syncConverter class, passing in the name of the Word Automation Service for your Farm.
                        SyncConverter sc = new SyncConverter(WORD_AUTOMATION_SERVICE);
                        //Pass in your User Token or credentials under which this conversion job is executed.
                        sc.UserToken = SPContext.Current.Site.UserToken;
                        sc.Settings.UpdateFields = true;

                        //Save format
                        sc.Settings.OutputFormat = SaveFormat.PDF; 

                        //Convert to PDF by opening the file stream, and then converting to the destination memory stream.
                        ConversionItemInfo info = sc.Convert(li.File.OpenBinaryStream(), destinationStream);

                        var filename = Path.GetFileNameWithoutExtension(li.File.Name) + ".pdf";
                        if (info.Succeeded)
                        {
                            //File conversion successful, then add the memory stream to the SharePoint list.
                            SPFile newfile = library.RootFolder.Files.Add(filename, destinationStream, true);
                        }
                        else if (info.Failed)
                        {
                            throw new Exception(info.ErrorMessage);
                        }
                     }
                }
            }
        }

After typing in the Document library name and clicking the button, the word document have been converted to PDF.

image

image

 

When building this demo, I did discover that if you have your Active Directory on the same box as your SharePoint then Word Automation Service will throw an error message as you try to convert documents, saying that:

“The file could not be converted; it may be corrupt or otherwise invalid (the conversion process failed). Please try opening the file in Microsoft Word, resaving it, and then resubmitting the file for conversion. If this does not resolve the issue, contact your system administrator.”

So firstly, I’m sorry to any developers out there that have used my earlier blogs to build themselves a Single SharePoint 2013 server farm. And secondly thank you to Karsten Pohnke who blog post confirmed why I was getting the error message. http://www.ilikesharepoint.de/2014/07/sharepoint-word-automation-service-does-not-work-file-may-be-corrupted/

Link to Sample code : OneDrive

Setting up PowerPoint Automation Services for SharePoint 2013


  1. Setting up Word Automation Services for SharePoint 2013
  2. Setting up PowerPoint Automation Services for SharePoint 2013
  3. Converting a Library’s Word Documents to PDF
  4. Converting a Library’s PowerPoint documents to PDF
  5. Using ITextSharp to Merge PDF’s into one
  6. Using ITextSharp to create basic PDF page from item property

Microsoft PowerPoint Automation Services is a new feature of Microsoft SharePoint Server 2013. Using PowerPoint Automation Services, you can convert PowerPoint (.pptx and .ppt) file to supported document formats (.pptx, .pdf, .xps, .jpg and .png).This blog post will walk you through the steps of setting up your SharePoint 2013 on premise farm to use PowerPoint Automation Services. Unlike with Word Automation Services, this cannot be configured from inside Central Admin.

clip_image002

To create this service PowerShell must be used. This feature is not available with SharePoint Online.Before you start, ensure in your AD you have created a service account. Mine is called PowerPointAutomation.

  1. Open up a PowerShell command window or PowerShell ISE.
  2. First we need to create the Managed Account in SharePoint. This can either be done by directly in SharePoint Central Admin by registering a new Managed Account https://<CentralAdminUrl>/_admin/ManagedAccounts.aspx
    clip_image004
    clip_image006
    Or we can do it with PowerShell

    add-pssnapin Microsoft.SharePoint.Powershell
    
    $cred = Get-Credential
    New-SPManagedAccount -Credential $cred
    

    When run, this will display a Credential box, put in here your Service account and password

    clip_image008

    Your Managed Account will now be registered with SharePoint.

  3. Now we can run the following script to create the PowerPoint Automation Service and Proxy. (Remember to change your service account)
    add-pssnapin Microsoft.SharePoint.Powershell
    
    #Create a new Service Application Pool Account
    New-SPServiceApplicationPool -Name "PowerPointAutomationAppPool" -Account cc\PowerPointAutomation
    #Get the Service Application pool
    $pool = Get-SPServiceApplicationPool | where {$_.Name -like "PowerPointAuto*"}
    #Create the PowerPoint Conversion Service
    $ppt = New-SPPowerPointConversionServiceApplication "PowerPoint Conversion Service" -ApplicationPool $pool
    #Create the Proxy for Service
    New-SPPowerPointConversionServiceApplicationProxy "PowerPoint Conversion Service Proxy" -ServiceApplication $ppt –AddToDefaultGroup
    

    Once complete your powershell window should look similar to mine below.

    clip_image010

    Also if you navigate in Central Administration back to your Service Applications, you will now see the PowerPoint Conversion Service Application and Proxy within the list.
    clip_image012

  4. For the PowerPoint Conversion Service to work, you will need create a new folder called PowerPointConversion on the server running the PowerPoint Conversion Service in the following location. C:\ProgramData\Microsoft\SharePoint. This folder directory is a hidden folder and you will need to show Hidden items in the ribbon of explorer to see it.
    clip_image014

    clip_image016
    clip_image018
  5. Once you have created this folder, view the security on this folder (Right click folder, click Properties, view the Security Tab) and give WSS_WPG Modify permissions. This will allow the Web Application pool service account to create folders and files within the newly created PowerPointConversion folder, which is required to convert a PowerPoint document.
  6. Lastly in Central Administration, under system Settings click on Manage Services on Server, and ensure that you have started PowerPoint Conversion Service.image

In Part 4 of these blog posts I will demo how to code against the PowerPoint Automation Services to convert PowerPoint Documents to PDF.

Setting up Word Automation Service for SharePoint 2013


  1. Setting up Word Automation Services for SharePoint 2013
  2. Setting up PowerPoint Automation Services for SharePoint 2013
  3. Converting a Library’s Word Documents to PDF
  4. Converting a Library’s PowerPoint documents to PDF
  5. Using ITextSharp to Merge PDF’s into one
  6. Using ITextSharp to create basic PDF page from item property

Word Automation Services for SharePoint 2013 is a SharePoint Service Application and it gives developers a way to perform server-side conversion of documents that are supported by Microsoft Word. This service was available in SharePoint 2010, however the newer version now allows on demand file conversions and converting streams to files. (Steams with Word Automation Service can only be used with on demand conversions, not Timer job-based requests.

This blog post will walk you through the steps of setting up your SharePoint 2013 on premise farm to use Word Automation Services. This feature is not available with SharePoint Online.

Before you start, ensure in your AD you have created a service account. Mine is called WordAutomation.

  1. Open Central Administration
  2. Under Application Management > Service Applications select Manage service applications
    image
  3. In the ribbon bar area, click New > Word Automation Services
    image
  4. On the Create New Word Automation Services Application dialog. Fill in:
    a. Name: WAS
    b. Create New Application pool: WASAppPool
    c. Security Account for Application Pool – Configurable: [Domain]\WordAutomation
        (Note: You might have to Register your new Managed account first, before it shows up in your Configurable Drop down list)
    d. Add to Default Proxy List: Tick
  5. Click Next
  6. On the next screen enter:
    a. Database Server: [SQL Server]
    b. Database Name: WASDatabase
  7. Click Finish
  8. Once created your Word Automation Services and Proxy will be displayed in the Service Applications list.

image

When you click WAS link you will be taken through to the configuration of the Word Automation Services. You will not need to change any of this to start using the Word Automation Services, however it is useful looking over these settings in case you need to change them in the future.

image

 

  • Lastly, in Central Administration, under System Settings, click on Services on Server, and ensure that you have Started the Word Automation Services.
    image

 

In Part 3 of these blog posts I will demo how to code against the Word Automation Services to convert Word Documents to PDF.