Thursday, January 13, 2011

SharePoint 2010 : Convert Document to PDF using Word Automation Service

Jan 13th 2011 at SharePointPro.SG (SharePoint User Group) i presented Word Automation Services the step by step walk though give below.

Objective : This session you will lean creating custom solution to convert Word documents to PDF using 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.

image

Select as as farm solution. (Word Service API’s dose not work with Sandboxed solutions)

image

Select List Definition as a Document Library and click Finish.

Ensue that you BaseType =1 and Type = 10000

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListTemplate
Name="ListDefinition1"
Type="10000"
BaseType="1"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="110"
DisplayName="WToXPS - ListDefinition1"
Description="My List Definition"
Image="/_layouts/images/itdl.png"
DocumentTemplate="121"/>
</Elements>



Right Click project and Add new item Select Event Receiver


image


Event Source  - Select an item was added and click finish.


image


Add project reference and click browse go to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI path and select Microsoft.Word.Server.dll file as reference and click OK.


image


Add following using statement on .CS file

using Microsoft.Office.Word.Server.Conversions;

Click Ctrl-K and Ctrl-S and select if statement


image


Insert following code :

if (properties.ListItem.Name.Contains(".docx") || properties.ListItem.Name.Contains(".doc")) 
{
ConversionJob jobConversion;
ConversionJobSettings jobSettings;
string wordDoc;
string pdfDoc;

jobSettings = new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;

jobConversion = new ConversionJob("Word Automation Services", jobSettings);
jobConversion.UserToken = properties.Web.CurrentUser.UserToken;

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

if (properties.ListItem.Name.Contains(".docx"))
{
pdfDoc = wordDoc.Replace(".docx", ".pdf");
}
else
{
pdfDoc = wordDoc.Replace(".doc", ".pdf");
}

jobConversion.AddFile(wordDoc, pdfDoc);
jobConversion.Start();
}

Now i converted the document to PDF i want discard the original document. Use ConversionJobStatus to monitor the ConversonJob process and once the process completed delete the document

 ConversionJobStatus status = new ConversionJobStatus("Word Automation Services",
jobConversion.JobId, null);

while (true)
{
Thread.Sleep(5000);
status = new ConversionJobStatus("Word Automation Services", jobConversion.JobId,
null);

if (status.Count == status.Succeeded + status.Failed)
{
properties.ListItem.Delete();
}
}



Right click Build and deploy your project


Upload files to Document Library


image


The code run  and generated PDF and Deleted the word document.


image


Download the full solution here at  http://cid-d1e9ef060fcd5fa2.office.live.com/self.aspx/Public/WordToPDFCoversion.zip

2 comments:

Krishna subhash bandaru said...

when i run this getting an error :

Error occurred in deployment step 'Recycle IIS Application Pool': Cannot connect to the SharePoint site: http://intranet.zahid.com/. Make sure that this is a valid URL and the SharePoint site is running on the local computer. If you moved this project to a new computer or if the URL of the SharePoint site has changed since you created the project, update the Site URL property of the project.

Unknown said...

Hi Krishna,

I think you download source and trying run… Please change Intranet site name to your site name.

How to change : Project in the Solution Explorer, from the Properties window below you can set the your SharePoint site to deploy to