converting word documents to pdf using sharepoint server 2010 and word automation services

Upload: sreedhar-konduru

Post on 04-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    1/12

    Converting Word Documents to PDF Using

    SharePoint Server 2010 and Word Automation

    Services

    Summary: Learn to programmatically convert Word documents to PDF format on the server by using

    Word Automation Services with SharePoint Server 2010. (9 printed pages)

    Applies to: Microsoft SharePoint Server 2010 | Microsoft Business Connectivity Services| Microsoft Visual

    Studio 2010

    Published: January 2010

    Provided by: Michael Case,iSoftStone

    SharePoint 2010 Word 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:

    1. Creating a SharePoint 2010 list definition application solution in Visual Studio 2010.2. Adding a reference to the Microsoft.Office.Word.Serverassembly.

    3. Adding an event receiver.4. Adding the sample code to the solution.

    Creating a SharePoint 2010 List Definition Application in Visual Studio 2010

    http://www.isoftstone.com/en/index.htmhttp://www.isoftstone.com/en/index.htmhttp://www.isoftstone.com/en/index.htmhttp://www.isoftstone.com/en/index.htm
  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    2/12

    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

    1. Start Microsoft Visual Studio 2010 as an administrator.2. From the FileMenu, point to the Projectmenu and then click New.3. In the New Project dialog box select the Visual C# SharePoint 2010 template type in the Project

    Templates pane.

    4. Select List Definitionin the Templatespane.5. Name the project and solution ConvertWordToPDF.

    Figure 1. Creating the Solution

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    3/12

    6. To create the solution, click OK.7. Select a site to use for debugging and deployment.8. Select the site to use for debugging and the trust level for the SharePoint solution.

    Note:

    Make sure to select the trust level Deploy as a farm solution. If you deploy as a sandboxed

    solution, it does not work because the solution uses the Microsoft.Office.Word.Server

    assembly. This assembly does not allow for calls from partially trusted callers.

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    4/12

    Figure 2. Selecting the trust level

    9. To finish creating the solution, click Finish.

    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.

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    5/12

    To add a reference to the Microsoft Office Word Server Assembly

    1. In Visual Studio, from the Projectmenu, select Add Reference.2. Locate the assembly. By using the Browse tab, locate the assembly. The

    Microsoft.Office.Word.Serverassembly is located in the SharePoint 2010 ISAPI folder. This is

    usually located at C:\Program Files\Common Files\Microsoft Shared\Web Server

    Extensions\14\ISAPI. After the assembly is located, click OKto add the reference.

    Figure 3. Adding the Reference

    Adding an Event Receiver

    This article uses an event receiver that uses the Microsoft.Office.Word.Serverassembly to create

    document conversion jobs and add them to the Word Automation Services conversion job queue.

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    6/12

    To add an event receiver

    1. In Visual Studio, on the Projectmenu, click Add New Item.2. In the Add New Item dialog box, in the Project Templatespane, click the Visual C# SharePoint

    2010 template.

    3. In the Templatespane, click Event Receiver.4. Name the event receiver ConvertWordToPDFEventReceiverand then click Add.

    Figure 4. Adding an Event Receiver

    5. The event receiver converts Word Documents after they are added to the List. Select the An itemwas addeditem from the list of events that can be handled.

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    7/12

    Figure 5. Choosing Event Receiver Settings

    6. Click Finishto add the event receiver to the project.Adding the Sample Code to the Solution

    Replace the contents of the ConvertWordToPDFEventReceiver.cs source file with the following code.

    VB

    C#

    C++

    F#

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');
  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    8/12

    JScript

    Copy

    usingSystem;

    usingSystem.Security.Permissions;

    usingMicrosoft.SharePoint;using Microsoft.SharePoint.Security;

    using Microsoft.SharePoint.Utilities;

    using Microsoft.SharePoint.Workflow;

    using Microsoft.Office.Word.Server.Conversions;

    namespaceConvertWordToPDF.ConvertWordToPDFEventReceiver

    {

    ///

    ///List Item Events

    ///

    publicclassConvertWordToPDFEventReceiver : SPItemEventReceiver

    {

    ///

    ///An item was added.

    ///

    publicoverridevoidItemAdded(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;stringwordFile;

    stringpdfFile;

    // Initialize the conversion settings.

    jobSettings = newConversionJobSettings();

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    9/12

    jobSettings.OutputFormat = SaveFormat.PDF;

    // Create the conversion job using the settings.

    pdfConversion =

    newConversionJob("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();

    }

    }

    }}

    Word Automation Services provided with SharePoint Server 2010 enables you to create server-based

    document solutions. Combining the functionality that is provided by Word Automation Services with the

  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    10/12

    document content manipulation support provided with the Open XML SDK enables you to create rich

    document solutions that execute on the server that do not require Automation of the Word client

    application.

    Examples of the kinds of operations supported by Word Automation Services are as follows:

    Converting between document formats (e.g. DOC to DOCX) Converting to fixed formats (e.g. PDF or XPS) Updating fields Importing "alternate format chunks"

    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 bang 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.

    TheItemAddedevent 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.

    VB

    C#

    C++

    F#

    JScript

    Copy

    // 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 initializesConversionJobSettingsand

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemadded.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemadded.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemadded.aspxhttp://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjobsettings.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjobsettings.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjobsettings.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjobsettings.aspxhttp://%20codesnippet_setlanguage%28%27f/#');http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemadded.aspx
  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    11/12

    ConversionJobobjects to convert the document to the PDF format.

    VB

    C#

    C++

    F#

    JScript

    Copy

    //Variables used by the sample code.

    ConversionJobSettings jobSettings;

    ConversionJob pdfConversion;

    stringwordFile;

    stringpdfFile;

    // Initialize the conversion settings.

    jobSettings = newConversionJobSettings();

    jobSettings.OutputFormat = SaveFormat.PDF;

    // Create the conversion job using the settings.

    pdfConversion =

    newConversionJob("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.

    VB

    C#

    C++

    F#

    JScript

    Copy

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

    // and the destination PDF document.

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

    http://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjob.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjob.aspxhttp://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjob.aspx
  • 8/13/2019 Converting Word Documents to PDF Using SharePoint Server 2010 and Word Automation Services

    12/12

    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 ConversionJobis added to the Word Automation Services conversion job queue.

    VB

    C#

    C++

    F#

    JScript

    Copy

    // 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();

    http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');http://%20codesnippet_setlanguage%28%27f/#');