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


Top 5 Contributors of the Month
david stephan
Santhakumar Munuswamy
Fauzul Azmi
Asad Ali

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)
 $savePDF.close()
 $wordPDF.Quit()
 Stop-Process -processname  winword  -ErrorAction SilentlyContinue
 Stop-Process -processname  acrord32 -ErrorAction SilentlyContinue
 }

example1:
--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'

example2:
--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:

Responses

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