Friday 20 September 2013

SharePoint Power Shell - Convert word document to PDF


Here is Power Shell script to convert word document in SharePoint library to PDF using word automation service.
I Hope the code is self explanatory, if anybody needs any help let me know 


[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Word.Server")

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue


# Input parameters for the script

# Get the Word Automation Service Proxy
$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq "Word Automation Services Proxy" }
      Write-Host " Word Automation Proxy Name : " $wasp
#Create the Conversion job
     $conversionJob = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)
      Write-Host " Conversion Job created : " $conversionJob

# Get the web url
     $web = Get-SPWeb "http:// xyz:31000/"
      Write-Host " Accessing Site : " $web

# Set the credentials to use when running the conversion job.
     $conversionJob.UserToken = $web.CurrentUser.UserToken
      Write-Host " Accessing current user token"

# Conversion Job Name
    $conversionJob.Name = "Convert docx to PDF"
    $conversionJob.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
    $conversionJob.AddFile($wordFile,$pdfFile)

# Start the conversion job
      Write-Host " Conversion Job Started....  "
    $conversionJob.Start()

No comments:

Post a Comment