Thursday, January 13, 2011

SharePoint 2010 : Convert Document to PDF using Word Automation Service

Jan 13th 2011 at SharePointPro.SG (SharePoint User Group) i presented Word Automation Services the step by step walk though give below.

Objective : This session you will lean creating custom solution to convert Word documents to PDF using document library list item event receiver to call Word Automation Services to convert Word documents to PDF when they are added to the list. The event receiver checks whether the list item added is a Word document. If so, it creates a conversion job to create a PDF version of the Word document and pushes the conversion job to the Word Automation Services conversion job queue.


Select as as farm solution. (Word Service API’s dose not work with Sandboxed solutions)


Select List Definition as a Document Library and click Finish.

Ensue that you BaseType =1 and Type = 10000

<Elements xmlns="">
DisplayName="WToXPS - ListDefinition1"
Description="My List Definition"

Right Click project and Add new item Select Event Receiver


Event Source  - Select an item was added and click finish.


Add project reference and click browse go to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI path and select Microsoft.Word.Server.dll file as reference and click OK.


Add following using statement on .CS file

using Microsoft.Office.Word.Server.Conversions;

Click Ctrl-K and Ctrl-S and select if statement


Insert following code :

if (properties.ListItem.Name.Contains(".docx") || properties.ListItem.Name.Contains(".doc")) 
ConversionJob jobConversion;
ConversionJobSettings jobSettings;
string wordDoc;
string pdfDoc;

jobSettings = new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;

jobConversion = new ConversionJob("Word Automation Services", jobSettings);
jobConversion.UserToken = properties.Web.CurrentUser.UserToken;

wordDoc = properties.WebUrl + "/" + properties.ListItem.Url;

if (properties.ListItem.Name.Contains(".docx"))
pdfDoc = wordDoc.Replace(".docx", ".pdf");
pdfDoc = wordDoc.Replace(".doc", ".pdf");

jobConversion.AddFile(wordDoc, pdfDoc);

Now i converted the document to PDF i want discard the original document. Use ConversionJobStatus to monitor the ConversonJob process and once the process completed delete the document

 ConversionJobStatus status = new ConversionJobStatus("Word Automation Services",
jobConversion.JobId, null);

while (true)
status = new ConversionJobStatus("Word Automation Services", jobConversion.JobId,

if (status.Count == status.Succeeded + status.Failed)

Right click Build and deploy your project

Upload files to Document Library


The code run  and generated PDF and Deleted the word document.


Download the full solution here at


Krishna subhash bandaru said...

when i run this getting an error :

Error occurred in deployment step 'Recycle IIS Application Pool': Cannot connect to the SharePoint site: Make sure that this is a valid URL and the SharePoint site is running on the local computer. If you moved this project to a new computer or if the URL of the SharePoint site has changed since you created the project, update the Site URL property of the project.

Unknown said...

Hi Krishna,

I think you download source and trying run… Please change Intranet site name to your site name.

How to change : Project in the Solution Explorer, from the Properties window below you can set the your SharePoint site to deploy to