SharePoint’s Word Automation Services is extremely powerful when it comes to converting document types and keeping the formatting.Combine this with the flexibility of the templates that OpenXML use and you have a very powerful combination capapble of ANYTHING.This is part of a Document Management System I developed for Nedbank that a Microsoft Field Engineer said was Impossible in SharePoint and asked R300 000 just for a few hours of his time for “analysisSharePoint 2010Word Automation Services available with SharePoint Server 2010 supports converting Word documents to other formats. This includes PDF. This article describes using a 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. This article describes the following steps to show how to call the Word Automation Services to convert a document:
Creating a SharePoint 2010 List Definition Application in Visual Studio 2010 This article uses a SharePoint 2010 list definition application for the sample code. To create a SharePoint 2010 list definition application in Visual Studio 2010
Adding a Reference to the Microsoft Office Word Server Assembly To use Word Automation Services, you must add a reference to the Microsoft.Office.Word.Server to the solution. To add a reference to the Microsoft Office Word Server Assembly
Adding an Event Receiver This article uses an event receiver that uses the Microsoft.Office.Word.Server assembly to create document conversion jobs and add them to the Word Automation Services conversion job queue. To add an event receiver
Adding the Sample Code to the Solution Replace the contents of the ConvertWordToPDFEventReceiver.cs source file with the following code.C 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
{
/// <summary>
/// List Item Events
/// </summary>
public class ConvertWordToPDFEventReceiver : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
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();
}
}
}
}
Examples of the kinds of operations supported by Word Automation Services are as follows:
This article contains sample code that shows how to create a SharePoint list event handler that can create Word Automation Services conversion jobs in response to Word documents being added to the list. This section uses code examples taken from the complete, working sample code provided earlier in this article to describe the approach taken by this article. The ItemAdded(SPItemEventProperties) event handler in the list event handler first verifies that the item added to the document library list is a Word document by checking the name of the document for the .doc or .docx file name extension. // Verify the document added is a Word document
// before starting the conversion.
if (properties.ListItem.Name.Contains(".docx")
|| properties.ListItem.Name.Contains(".doc"))
{
If the item is a Word document then the code creates and initializes ConversionJobSettings and ConversionJob objects to convert the document to the PDF format. C#
//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;
The Word document to be converted and the name of the PDF document to be created are added to the ConversionJob. // 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);
Finally the ConversionJob is added to the Word Automation Services conversion job queue. // 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();
|