Things hard and not so hard.... RSS 2.0
# Monday, September 20, 2010

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 2010.

image 

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 PDF…I could find none of these here :()

image
(yes the pdf icon needs work…)

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

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="http://schemas.microsoft.com/sharepoint/"> 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:

image

image

image

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.

Monday, September 20, 2010 9:06:48 PM (AUS Eastern Standard Time, UTC+10:00)  #    Comments [2] -
.NET Developer | SharePoint | 2010
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Blogroll
 AppFabric CAT
AppFabric Windows Server Customer Advisory Team - New Blog.
[Feed] BizTalk 2006 - Windows SharePoint Services adapter
BizTalk 2006 Sharepoint adapter!!
 Breeze SharePoint 2010 Bootcamp
Breeze SharePoint 2010 Bootcamp
[Feed] BTS 2006 R2/EDI
[Feed] Chris Vidotto (MS BTS Legend)
Needs no intro....
 Mark Daunt
BTS/SPS/.NET GURU!!!
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2013
Breeze
Sign In
Statistics
Total Posts: 588
This Year: 23
This Month: 4
This Week: 0
Comments: 270
All Content © 2013, Breeze