Monday, January 9, 2012

Bulk-Creating PDF Files from Word

 

PowerTip of the Day, from PowerShell.com:

 

To convert a whole folder full of MS Word documents to PDF, here's a function that might help:

function Export-WordToPDF {

  param(

  [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]

  [Alias("FullName")]

  $path,

  $pdfpath = $null)

  process {

    if (!$pdfpath) {

      $pdfpath = [System.IO.Path]::ChangeExtension($path, '.pdf')

    }

    $word = New-Object -ComObject Word.Application

    $word.displayAlerts = $false

   

    $word.Visible = $true

    $doc = $word.Documents.Open($path)

    #$doc.TrackRevisions = $false

    $null = $word.ActiveDocument.ExportAsFixedFormat($pdfpath, 17, $false, 1)

    $word.ActiveDocument.Close()

    $word.Quit()

  }

}

Use it like this:

PS> Dir c:\folder -Filter *.doc | Export-WordToPDF

Thanks

5 comments:

  1. This post explains how you convert entire Word file into PDF file. The program given for conversion is not very difficult. All you need to do is just understand the basic logic behind the given program. This post is very useful. Thanks for sharing this idea with us.
    digital signature software

    ReplyDelete
  2. Thanks for this great script.

    A few corrections/additions based on using Word 2010 with it:
    - displayAlerts needs 0 not $false as a parameter
    - open documents read only - add parameters ", [ref] $false, [ref] $false" to the open command
    - save as PDF/A archive format: Parameterlist for ExportAsFixedFormat becomes ExportAsFixedFormat($pdfpath, 17, $false, 0, 0, 0, 0, 0, $false, $false, 0, $false, $true, $true)
    - $word.activeDocument.Close(): Add parameter [ref] 0 to avoid saving the unchanged doc/docx.
    - $word.Quit(): Add [ref] $false to avoid saving any changes

    ReplyDelete
    Replies
    1. Hi P.

      thanks a lot for the updates,

      :)

      thanks
      aman

      Delete

Note: Only a member of this blog may post a comment.