Developing Applications with SharePoint 2010 Word Automation Services

Common requirement is to convert doc to PDF and for that we often go for the third party software / DLL. We now also have an option to convert word document in PDF in SharePoint 2010.

Well, now with SharePoint 2010 word automation services, it is possible to convert word document to the PDF file. Word automation service is only available in SharePoint server 2010 not in SharePoint foundation server.

Word automation service can convert document to one format to another format. You can also do this programmatically as well.

Input Formats Output Formats
Word 97-2003 Document (DOC) and Word 97-2003 Template (DOT)
Rich Text Format (RTF)
Single File Web Page (MHTML)
HTML
Word 2003 XML
Word 2007/2010 XML
PDF
XPS
Office Open XML (DOCX, DOCM)
Word 97-2003 Document (DOC)
Rich Text Format (RTF)
Single File Web Page (MHTML)
Word 2007/2010 XML

To work on this concept, we need to make sure that word automation service is started on the server. Go to your central administration – Application management – Services on server and see that it is started, if not, then starts it.

With Code

Just to show you how it works, I am using document in the same library and outputting the resulting converted documents to the same library.Output library can also be different from the site.

You need to include one DLL reference in your project and that is (which will be there in ISAPI folder) Microsoft.Office.Word.Server.Conversions.ConversionJob.

Once you execute this code, you might have to wait for couple of minutes or more. It depends on the type of file you are converting. Check out different options in conversions.SaveFormat. There are different formats available.

private void ConvertDocFileToPDF()
{
try
{
ConversionJobSettings settings;
ConversionJob job;
settings = new ConversionJobSettings();
settings.OutputFormat = SaveFormat.PDF;
string wordAutomationServiceName = “Word Conversion Services”;

job = new ConversionJob(wordAutomationServiceName, settings);

job.Name = “Convert docx to PDF”;

job.UserToken = SPUserToken.SystemAccount;

job.AddFile(“http://sharepointserver/sites/JRDW/Word2PDF/InvoiceTracking.docx”,
http://sharepointserver/sites/JRDW/Word2PDF/InvoiceTracking.pdf”);

job.Start();
lblStatus.Text = “Conversion job started!”;
ConversionJobStatus status = new ConversionJobStatus(wordAutomationServiceName, job.JobId, null);

if (status.Count == status.Succeeded)
{
lblStatus.Text = “Document Conversion Succeeded”;

}
else if (status.Count == status.Failed)
{
lblStatus.Text = “Document Conversion Failed”;

}
}
catch (Exception ex)
{

lblStatus.Text = “Error:<br/>”;
lblStatus.Text += ex.Message;
}
}

To run the job immediately use the below code 

foreach (var services in spSite.WebApplication.Farm.Services)
{
if (service.TypeName == “Word Automation Services”)
{
foreach (var jobDefinition in service.JobDefinitions)
{
if (jobDefinition.Name == “Word Automation Services”)
{
jobDefinition.RunNow();
break;
}
}
}
}

With PowerShell

You can use the below script to test the Word Automation Service. It basically tells the Word Automation Service which document to convert and the destination of this document. Save this script as (something like) “convert2pdf.ps1″ and run it using the SharePoint 2010 Management Shell.

# Input parameters for the script
$wordFile=”http://sharepointserver/sites/JRDW/Word2PDF/InvoiceTracking.docx&#8221;
$pdfFile=”http://sharepointserver/sites/JRDW/Word2PDF/InvoiceTracking.pdf&#8221;
# Get the Word Automation Service Proxy
$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq “Word Conversion Services” }
#Create the Conversion job
$conversionJob = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)
# Get the web url
$web = Get-SPWeb “http://sharepointserver/&#8221;
# Set the credentials to use when running the conversion job.
$conversionJob.UserToken = $web.CurrentUser.UserToken
# Conversion Job Name
$conversionJob.Name = “Convert docx to PDF”
$conversionJob.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
$conversionJob.AddFile($wordFile,$pdfFile)
# Start the conversion job
$conversionJob.Start()

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: