Team blog of MCS @ Middle East and Africa

This blog is created by Microsoft MEA HQ near shoring team, and it aims to share knowledge with the IT community.With its infrastructure and development sides,It brings to you the proven best practices and real world experiences from Subject Matter Experts
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

Using SharePoint 2010 Word Automation Services to convert Word documents to PDF files

Using SharePoint 2010 Word Automation Services to convert Word documents to PDF files

  • Comments 4
  • Likes

A very interesting feature of SharePoint 2010 is the Word automation services. Word automation services allows you to convert between multiple document formats which runs as a batch job. This is very useful in many server side scenarios where customers require to archive documents, protect them from editing among many others.

I am Sharing the following code snippets that will allow you to convert between multiple document formats.... 

private void ConvertDocFileToPDF(SPFile filex)
{
    // The job and its settings
    ConversionJobSettings settings;
    ConversionJob job;
    // Set the conversion settings, the enumeration allows you to convert to PDF, RTF, XPS and other word formats
    settings = new ConversionJobSettings();
    settings.OutputFormat = SaveFormat.PDF;
    // Create th conversion job...this must be the name of the job as defined in SharePoint farm
    job = new ConversionJob(ConfigurationHelper.Instance.DocumentConversionService, settings);
    // Most propably, you will want this job to run with system credintials, however, you can use other
    job.UserToken = SPUserToken.SystemAccount;
    // Add your file to the conversion job...in this example, you can also save the converted file to an attachement!!! IN the below code, you might use the SPContext object to get the root URL of your website
    string pdfFile = filex.Url.Replace("docx","pdf")
    job.AddFile("http://<your site URL>/"+ filex.Url, "http://<your site URL>/" + pdfFile);

    // Start the conversion job
    job.Start();
}

You will need to add reference to the assembly "Microsoft.Office.Word.Server" which you will find in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\".

To ensure that your code runs with no problem, ensure that you did setup a Word Automation Service in your SharePoint farm by checking the service applications from the central administration, or create new one...

 

 

 

 

 

 

 

 

 

 

when you want to test it, run the job "Word Automation Services Timer Job" which will allow you to test the conversion immediately. If you cannot find this job, this means that the configuration is missed up and your code will probably not run, so check this job first before running your code.

 Happy coding:)

Comments
  • I tried this one in our company:

    www.vialutions.com/.../vconvert

    and it is great! We bought it couple months ago and it is working perfect!

    Best Regards

    Jim

  • It throws an error..

    The name 'ConfigurationHelper' does not exist in the current context

  • is it possibly to use this in Sharepoint foundation 2010?

  • Hi. This service here allows you to easily edit your PDF documents.

    http://goo.gl/x4bL1R

    You can fill out PDF form, save it, fax it, and email it.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment