The scene looks like this – while teaching a SharePoint 2010 class I
decided to build a Feature that used one of the OOTB Service Applications of SharePoint


I decided to create a PDF Converter ’Feature’ that used the Word Automation Services
hosted in SharePoint 2010. Looking into the Word Automation Services, I’d say that
if you’ve already got a PDF creation process going, then stick with it as it appears
this service is pretty simple. However if you’ve got nothing, then Word Automation
Services will be great!
(Having spent a previous life in a graphics company, there are many options that go
with creating just that perfect PDFI could find none of these here :()

(yes the pdf icon needs work)

So I created a VS.NET 2010 Solution with a Feature.

The PDFConverter.cs is where the crux of the work is – the rest of
the solution is working out just the right spot to call it.

Couple of Interesting Points about the Solution

1. ScriptLink – using this from a CustomAction within an Elements
file allows to inject Script into a site where the Feature is Activated. There is
also ScriptBody that allows you to inject script code right there.

2. RegistrationType – being declared as a FileType, currently this
will work with docx. Feel free to experiment and extend out.
Also, seeing this action is activated on a list item, we need to track what list it
came from {ListId} and the item in that list {ItemId} which
is an integer.

1 <Elements xmlns=""> 2 <CustomAction Id="MicksPDFScript" 3 ScriptSrc="~site/_layouts/WordAutomationServices/Scripts/MoveItMoveIt.js" 4 Location="ScriptLink"> 5 </CustomAction> 6 7 <CustomAction Id="MicksPDFConverter" 8 RegistrationType="FileType" 9 RegistrationId="docx" 10 Location="EditControlBlock" 11 Sequence="106" 12 Title="Convert to PDF" 13 ImageUrl="~site/_layouts/WordAutomationServices/Images/pdf.gif" 14 > 15 <UrlAction Url="javascript:MicksOpenDialog('{ListId}','{ItemId}');"/> 16 </CustomAction> 17 18 </Elements> 19

3. Code that actually does the work – is pretty simple really with Folders
and entire Document Libraries able to be passed to the Conversion Job.

One annoying thing is that below in Line15, conversionJob.Start() is
called, really a job gets created and added to the Job Timer queue. Regardless of
what goes on, the Started property always returns true.

Typically I’ve found the Timer Job to kick in every 5 mins to process the conversions
and eventually a PDF file is seen in the library.

1 public bool Convert(string srcFile,string dstFile) 2 { 3 4 //create references to the Word Services. 5 var wdProxy = (WordServiceApplicationProxy)SPServiceContext.Current.GetDefaultProxy(typeof(WordServiceApplicationProxy)); 6 var conversionJob = new ConversionJob(wdProxy); 7 8 conversionJob.UserToken = SPContext.Current.Web.CurrentUser.UserToken; 9 conversionJob.Name = "Micks PDF Conversion Job " + DateTime.Now.ToString("hhmmss"); 10 conversionJob.Settings.OutputFormat = SaveFormat.PDF; 11 conversionJob.Settings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite; 12 13 conversionJob.AddFile(srcFile, dstFile); 14 15 conversionJob.Start(); 16 return (conversionJob.Started); 17 18 }

A couple of screen shots in action:

Of course this is not production ready, but it should give you a great start in getting
there. To start, simply install and Activate the Feature on a site collection to see
the functionality.

Go to a document library and activate the item drop down to see the Convert
to PDF
option. Must be a DOCX file currently.

Grab the VSNET2010 Solution and go for it – have fun.