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.
Select as as farm solution. (Word Service API’s dose not work with Sandboxed solutions)
Select List Definition as a Document Library and click Finish.
Ensue that you BaseType =1 and Type = 10000
DisplayName="WToXPS - ListDefinition1"
Description="My List Definition"
Right Click project and Add new item Select Event Receiver
Event Source - Select an item was added and click finish.
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.
Add following using statement on .CS file
Click Ctrl-K and Ctrl-S and select if statement
Insert following code :
if (properties.ListItem.Name.Contains(".docx") || properties.ListItem.Name.Contains(".doc"))
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;
pdfDoc = wordDoc.Replace(".docx", ".pdf");
pdfDoc = wordDoc.Replace(".doc", ".pdf");
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",
status = new ConversionJobStatus("Word Automation Services", jobConversion.JobId,
if (status.Count == status.Succeeded + status.Failed)
Right click Build and deploy your project
Upload files to Document Library
The code run and generated PDF and Deleted the word document.
Download the full solution here at http://cid-d1e9ef060fcd5fa2.office.live.com/self.aspx/Public/WordToPDFCoversion.zip
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.
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
Thanks, great work!
Here is another solution to create PDF documents:
The solution is dynamically creating a PDF document from a SharePoint list using a template engine to customize the PDF output.
Post a Comment