By: Kathryn Bartlett
In this blog I will walk through the process of programmatically creating a Word document, using the Open XML SDK 2.0, uploading this document to SharePoint 2010, and then using Word Automation Services to convert this document into a PDF. For this example, all of the code will be running from a Console Application created with Visual Studio 2010.
Creating the Console Application
First we must create the Console Application and make sure all of the appropriate References are added. In Visual Studio 2010, this can be done easily by clicking on File -> New -> Project.

Then in the Windows section underneath Visual C#, select Console Application. Make sure .NET Framework 3.5 is selected, enter a name, and hit OK.

Now we need to make sure the application’s Platform target is set to Any CPU. To do this, right-click Properties and then Open:

Select the Build tab and make sure to set the Platform target to Any CPU.

Now that we’ve created the project successfully, we need to add the following references:
· Microsoft.SharePoint
· DocumentFormat.OpenXml
· WindowsBase
· Microsoft Office 2010 component
In order to add the DocumentFormat.OpenXML reference, we need to download the OpenXML SDK 2.0. This can be found at the following URL: http://www.microsoft.com/download/en/details.aspx?id=5124
Offered on this site is both a Productivity tool, as well as the actual SDK. For purposes of this blog, all we need is the SDK. The tool is helpful for generating more complex documents, as it allows one to reverse-engineer the code.
To add these references, right-click References and select Add Reference…

Building the Code
As this is a simple example, we will work solely out of the Main method. First we need to connect to SharePoint to get to our document library. We also need to make sure the web is set to allow unsafe updates.

Next we will build the code that will generate the Word document. In order to upload into SharePoint, we need to create the document off a memory stream. Using OpenXML, you create a WordprocessingDocument. This WordprocessingDocument needs to contain a MainDocumentPart. The MainDocumentPart will need to contain a Document. The Document will need to contain a Body. This body is where your paragraphs will lie.

Next, we will create two simple paragraphs. As mentioned earlier, these will need to be appended to the Body. Each paragraph needs to contain a Run which needs to contain Text.

These Paragraph, Run, and Text objects can contain properties to add styles, fonts, etc. The productivity tool mentioned earlier makes determining what these properties should look like much simpler.
Now we need to upload this document into our SharePoint library.

Using Word Automation Services, we will now convert this Word document into a PDF with the following code.

By default, the Word Automation Service Timer Job runs every 15 minutes. Therefore, the PDFs you try to make will actually be created and uploaded into the library every 15 minutes – you will not see your PDF instantly upon running this code. You can change the frequency with which this job runs by going into Central Administration. Select Monitoring, then Review Job Definitions. Click on the job Word Automation Services Timer Job and select the frequency appropriate for your scenario.
By: Kathryn Bartlett