How to convert docx to pdf document in SharePoint 2010 using PowerShell - Part1



Word Automation Services is a new feature available in SharePoint 2010. It supports converting Word documents to other formats. Here we are going to convert a word document into a pdf document.

This article describes the following steps to show how to call the Word Automation Services to convert a document:

  • Add a word document to the SharePoint Shared Documents.
  • Create ConvertWordToPDF.ps1 file.

Add a word document in the Shared Documents:

1.gif

Create a ConvertWordToPDF.ps1 file:

$assembly = ("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c","Microsoft.Office.Word.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" )
$code = @"

using System;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;

namespace Vijai.Script.WordConversion
{
    public static class ConvertWordToPDFDocument
    {
        public static void Conversion()
        {
           using (SPSite spSite = new SPSite( "
http://ServerName:4040/"))

            {
                //Defines a collection of settings for all conversions within a single conversion job.
                ConversionJobSettings jobSettings=new ConversionJobSettings();
                jobSettings.OutputFormat = SaveFormat.Document;
                //Represents a collection of file conversions (of single files or entire libraries) that share a common set of conversion properties and are logically tracked as a single unit.
                ConversionJob pdfConversion = new ConversionJob("Word Automation Services", jobSettings);
                // Set the credentials to use when running the conversion job.
                pdfConversion.UserToken = spSite.UserToken;
                pdfConversion.AddFile("http://ServerName:4040/Shared%20Documents/ExternalList.docx",
                    "http://ServerName:4040/Shared%20Documents/ExternalList.pdf");
                pdfConversion.Start();
            }
        }
    }
}

"@

Add-Type -ReferencedAssemblies $assembly -TypeDefinition $code -Language CSharp

[Vijai.Script.WordConversion.ConvertWordToPDFDocument]::Conversion()

Open the SharePoint 2010 Management Shell as an Administrator. Run the ConvertWordToPDF.ps1 file.

Go to the Shared Documents in SharePoint 2010; the pdf file will be created as shown in the following.

2.gif