Using SharePoint 2013 Word Automation Services to convert Word file to PDF


In below example, we will demonstrate the solution of converting Word file to PDF file by using the SharePoint 2013 Word Automation Services.

First of all, make sure that the service is configured and running, you can refer to Configuring Word Automation Services for Development.

Initially, add a reference to “Microsoft.Office.Word.Server.dll” which normally resides in:

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.Office.Word.Server.dll

Second, below is the code for taking the SPFile object and pass it to the conversion method, and then wait for the conversion to finish to proceed:

  1. public bool ConvertWordFileToPdf(SPFile file, SPUser user) {  
  2.     if (file == nullreturn false;  
  3.     var fileExtention = System.IO.Path.GetExtension(file.Url);  
  4.     var filePath = string.Format(“{0}/{1}”, file.Web.Site.Url, file.Url);  
  5.     var pdfPath = System.IO.Path.ChangeExtension(filePath, “pdf”);  
  6.     if (fileExtention == “.docx” || fileExtention == “.doc”) {  
  7.         var conversionItemInfo = ConvertWordFileToPDF(filePath, pdfPath, user);  
  8.         if (conversionItemInfo == null) {  
  9.             throw new AccessViolationException(“Word automation services is not accessible”);  
  10.         } else {  
  11.             while (conversionItemInfo.InProgress) {}  
  12.             if (conversionItemInfo.Failed) {  
  13.                 return false;  
  14.             }  
  15.             if (conversionItemInfo.Succeeded) { // Proceed with your own code here  
  16.             }  
  17.         }  
  18.     }  
  19. }  

     

    Finally, here is the conversion method which creates the conversion job settings which takes care if the conversion for us:

  20. public ConversionItemInfo ConvertWordFileToPDF(string sourceFilePath, string destinationFilePath, SPUser user) {  
  21.     try {  
  22.         var serviceContext = SPServiceContext.GetContext(SPContext.Current.Web);  
  23.         var wordProxy = (WordServiceApplicationProxy) serviceContext.GetDefaultProxy(typeof(WordServiceApplicationProxy));  
  24.         var jobSettings = new ConversionJobSettings();  
  25.         jobSettings.OutputFormat = SaveFormat.PDF;  
  26.         var converter = new SyncConverter(wordProxy, jobSettings);  
  27.         converter.UserToken = user.UserToken;  
  28.         return converter.Convert(sourceFilePath, destinationFilePath);  
  29.     } catch (Exception ex) { // Log the exception    
  30.         return null;  
  31.     }  
  32. }  

     

    Notes:

  • Make sure that the used SPUser has access to the Word Conversion Service.
  • This method output the exact output of Microsoft Word save as PDF.
  • In the case of PDF conversion, you can use some libraries to optimize the file size (if needed), personally I have tried some but didn’t notice a remarkable difference in the case of text based Word files.
  • Here are the available save formats as well:
  1. namespace Microsoft.Office.Word.Server.Conversions { 
  2. // Summary: //     Represents the supported save formats for output files.  
  3.     public enum SaveFormat { 
  4. // Summary: //     Use the file extension of the output file to determine output format.  
  5.         Automatic = 0, // // Summary: //     Save output file as a Word Document (.docx).  
  6.             Document = 1, // // Summary: //     Save output file as a Word Macro-Enabled Document (.docm).  
  7.             DocumentMacroEnabled = 2, // // Summary: //     Save output file as a Word 97-2003 Document (.doc).  
  8.             Document97 = 3, // // Summary: //     Save output file as a Word Template (.dotx).  
  9.             Template = 4, // // Summary: //     Save output file as a Word Macro-Enabled Template (.dotm).  
  10.             TemplateMacroEnabled = 5, // // Summary: //     Save output file as a Word 97-2003 Template (.dot).  
  11.             Template97 = 6, // // Summary: //     Save output file as a Single File Web Page (.mht).  
  12.             MHTML = 7, // // Summary: //     Save output file as a PDF (.pdf).  
  13.             PDF = 8, // // Summary: //     Save output file as a Rich Text Format document (.rtf).  
  14.             RTF = 9, // // Summary: //     Save output file as a Word XML Document (.xml).  
  15.             XML = 10, // // Summary: //     Save output file as a XPS Document (.xps).  
  16.             XPS = 11, DocumentStrict = 12,  
  17.     }  
  18. }  
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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