Thursday, December 29, 2011

Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

Introducing Word Automation Services:
http://blogs.office.com/b/microsoft-word/archive/2009/10/26/introducing-word-automation-services.aspx

Over the last couple of months, we've posted about many of the exciting new features of Word 2010 – Co-authoring, the new Find experience, and the Word Web App. This week, at SharePoint Conference 2009, we announced one more (and one that I'm especially excited about): Word Automation Services.




In the post on framing the Word 2010 release, one of the pillars described is "Word Power in New Contexts". Word Automation Services is a big part of that pillar, and represents our desire to ensure that the power and functionality of Word is available beyond the desktop; in this case, by enabling developers to harness the capabilities of Word on the server as part of SharePoint 2010.



Word Automation Services

Have you ever wanted to convert .docx files into PDF? We've heard from many customers trying to perform server side conversions of Open XML files (.docx) into fixed formats (PDF and XPS) using the Word desktop application, and that's what motivated us to create Word Automation Services.



As a component of SharePoint 2010, Word Automation Services allows you to perform file operations on the server that previously required automating desktop Word:



•Converting between document formats (e.g. DOC to DOCX)

•Converting to fixed formats (e.g. PDF or XPS)

•Updating fields

•Importing "alternate format chunks"

•Etc.

If you've done any automation of Word, you're probably familiar with the challenges of doing so – challenges well documented by this Knowledge Base article: http://support.microsoft.com/kb/257757. With Word Automation Services, those challenges are a thing of the past:



•Reliability – The service was built from the ground up to work in a server environment, which means that you no longer have to worry about issues like dialog boxes that bring the process to a halt, expecting a user to provide input; creating interactive user accounts under which to run the application to avoid running into permissions issues, etc.

•Speed – The service is optimized to perform server-side file operations, and in doing so provides performance significantly better than existing solutions.

•Scalability – The service can take advantage of the processing power available on typical server hardware (multiple processors, additional memory). For example, although a single instance of WINWORD.EXE can only utilize a single core of processing power, with Word Automation Services, you can specify the number of simultaneous conversions (and the # of processing cores) to use based on the available hardware.

And you still have a solution that has 100% fidelity with respect to the Word desktop application – documents are paginated the same way on the server as they are on the client, ensuring that what you see on the client is what you get from the server.



In future posts, I'll spend some time digging into exactly how the service works, as well as each of these benefits of the service in further detail.




Summary: Learn to programmatically convert Word documents to PDF format on the server by using Word Automation Services with SharePoint Server 2010.


Sample Code:
using System;


using System.Security.Permissions;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Security;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.Workflow;



using Microsoft.Office.Word.Server.Conversions;



namespace ConvertWordToPDF.ConvertWordToPDFEventReceiver

{

///

/// List Item Events

///


public class ConvertWordToPDFEventReceiver : SPItemEventReceiver

{

///

/// An item was added.

///


public override void ItemAdded(SPItemEventProperties properties)

{

base.ItemAdded(properties);



// Verify the document added is a Word document

// before starting the conversion.

if (properties.ListItem.Name.Contains(".docx")



properties.ListItem.Name.Contains(".doc"))

{

//Variables used by the sample code.

ConversionJobSettings jobSettings;

ConversionJob pdfConversion;

string wordFile;

string pdfFile;



// Initialize the conversion settings.

jobSettings = new ConversionJobSettings();

jobSettings.OutputFormat = SaveFormat.PDF;



// Create the conversion job using the settings.

pdfConversion =

new ConversionJob("Word Automation Services", jobSettings);



// Set the credentials to use when running the conversion job.

pdfConversion.UserToken = properties.Web.CurrentUser.UserToken;



// Set the file names to use for the source Word document

// and the destination PDF document.

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

if (properties.ListItem.Name.Contains(".docx"))

{

pdfFile = wordFile.Replace(".docx", ".pdf");

}

else

{

pdfFile = wordFile.Replace(".doc", ".pdf");

}



// Add the file conversion to the conversion job.

pdfConversion.AddFile(wordFile, pdfFile);



// Add the conversion job to the Word Automation Services

// conversion job queue. The conversion does not occur

// immediately but is processed during the next run of

// the document conversion job.

pdfConversion.Start();



}

}

}

}

Important Links:
http://msdn.microsoft.com/en-us/library/ff181518.aspx


http://msdn.microsoft.com/en-us/library/ff742315.aspx

Word Automation Services:

http://msdn.microsoft.com/en-us/library/gg703645.aspx

Sample Code download:

http://code.msdn.microsoft.com/SharePoint-2010-Converting-7904ca12

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/661ab377-6f6a-40f0-8c83-8a0300a9a470

http://msdn.microsoft.com/en-us/library/gg703645.aspx

http://blogs.msdn.com/b/ericwhite/archive/2010/03/17/word-automation-services-determining-which-documents-failed-to-convert-using-c.aspx


Using Word Automation Services in a Workflow custom activity for SP Designer Workflows:

http://blogs.msdn.com/b/chandru/archive/2010/09/04/using-word-automation-services-as-a-workflow-custom-activity-in-sp-designer.aspx


What is the Difference between ‘Word Automation’ and ‘Word Automation Services’?:

http://blogs.msdn.com/b/ericwhite/archive/2010/12/07/what-is-the-difference-between-word-automation-and-word-automation-services.aspx




No comments:

Post a Comment