.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> PowerShell >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Convert word document to PDF using powershell

Posted By:Kemal AL GAZZAH       Posted Date: June 02, 2016    Points: 40    Category: PowerShell    URL: http://www.dotnetspark.com  

A simple way to convert word document to PDF using Powershell script

function convert($pdfname,$docName)
 Stop-Process -processname  winword  -ErrorAction SilentlyContinue
 Stop-Process -processname  acrord32 -ErrorAction SilentlyContinue
 # Kamel Gazzah
 # 26/05/2016
 # Converts a document word to PDF
 # $pdfname = "d:\ps\documents\doc1.pdf"
 # $docName = "d:\ps\documents\doc1.docx"

 $wordPDF = New-Object -ComObject Word.Application
 $wordPDF.Visible = $false
 $savePDF = $wordPDF.Documents.Open($docName, $false, $false)

 $savePDF.exportAsFixedFormat($pdfname, 17)
 Stop-Process -processname  winword  -ErrorAction SilentlyContinue
 Stop-Process -processname  acrord32 -ErrorAction SilentlyContinue

--first load your ps1 file
import-module 'd:\ps\documents\convert.ps1'
convert -pdfname 'd:\ps\documents\mydoc.pdf' -docname 'd:\ps\documents\doc1.docx'

--this sample converts the doc document, measure the execution time and store the result into a text file
import-module 'd:\ps\documents\convert.ps1'
Measure-command {convert -pdfname 'd:\ps\documents\mydoc1.pdf' -docname 'd:\ps\documents\doc1.docx'} > 'd:\ps\documents\execution-duration.txt'


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend