引数として指定した複数のPDFファイルを以下のように移動・変換するPowerShellスクリプトを作成してください

以下のPowerShellスクリプトは、指定したPDFファイルごとに同名のフォルダを作成し、PDFをそのフォルダに移動した後、Ghostscript を使用してPDFの全ページを画像化します。

param (
  [string[]]$pdfFiles  # 引数としてPDFファイルのパスを複数受け取る
)

# Ghostscriptの実行ファイルのパスを指定(適宜変更)
$gsPath = "C:\\\\Program Files\\\\gs\\\\gs10.02.0\\\\bin\\\\gswin64c.exe"

# Ghostscriptが存在するか確認
if (-Not (Test-Path $gsPath)) {
  Write-Host "Error: Ghostscriptが見つかりません: $gsPath"
  exit 1
}

foreach ($pdfFile in $pdfFiles) {
  if (-Not (Test-Path $pdfFile)) {
    Write-Host "Error: PDFファイルが見つかりません: $pdfFile"
    continue
  }

  # PDFファイル名(拡張子なし)を取得
  $pdfName = [System.IO.Path]::GetFileNameWithoutExtension($pdfFile)
  $pdfDir = [System.IO.Path]::Combine((Get-Item $pdfFile).DirectoryName, $pdfName)

  # フォルダ作成(存在しない場合)
  if (-Not (Test-Path $pdfDir)) {
    New-Item -ItemType Directory -Path $pdfDir | Out-Null
  }

  # PDFをフォルダ内に移動
  $newPdfPath = [System.IO.Path]::Combine($pdfDir, [System.IO.Path]::GetFileName($pdfFile))
  Move-Item -Path $pdfFile -Destination $newPdfPath

  # Ghostscriptを使用してPDFを画像化
  $outputPattern = [System.IO.Path]::Combine($pdfDir, "$pdfName-%03d.png")
  $gsCommand = "-dNOPAUSE -dBATCH -sDEVICE=png16m -r300 -sOutputFile=`"$outputPattern`" `"$newPdfPath`""

  Write-Host "Converting $newPdfPath to images..."
  Start-Process -FilePath $gsPath -ArgumentList $gsCommand -NoNewWindow -Wait
}

Write-Host "処理完了!"

使い方

以下のように、PowerShellでスクリプトを実行してください。

.\\\\convert_pdfs.ps1 "C:\\\\path\\\\to\\\\file1.pdf" "C:\\\\path\\\\to\\\\file2.pdf"

説明

  1. スクリプトの引数として複数のPDFを指定可能。
  2. PDFと同名のフォルダを作成し、そのフォルダ内にPDFを移動。
  3. Ghostscript (gswin64c.exe) を使ってPDFを300dpiのPNG画像に変換。

必要な環境

Ghostscriptのパスが異なる場合は $gsPath の値を変更してください。

引数にディレクトリパスが渡された場合、そのフォルダ内にある全てのPDFファイルを参照する処理を追加してください

以下のPowerShellスクリプトは、引数として渡されたパスがファイルかディレクトリかを判定し、ディレクトリの場合はその中の全PDFファイルを処理するように拡張しました。

param (
  [string[]]$paths  # 引数としてファイルまたはディレクトリのパスを受け取る
)

# Ghostscriptの実行ファイルのパスを指定(適宜変更)
$gsPath = "C:\\\\Program Files\\\\gs\\\\gs10.02.0\\\\bin\\\\gswin64c.exe"

# Ghostscriptが存在するか確認
if (-Not (Test-Path $gsPath)) {
  Write-Host "Error: Ghostscriptが見つかりません: $gsPath"
  exit 1
}

# PDFファイルのリストを格納する配列
$pdfFiles = @()

foreach ($path in $paths) {
  if (-Not (Test-Path $path)) {
    Write-Host "Error: 指定されたパスが存在しません: $path"
    continue
  }

  if (Test-Path $path -PathType Container) {
    # パスがディレクトリの場合、その中の全PDFファイルを取得
    $pdfFiles += Get-ChildItem -Path $path -Filter "*.pdf" -File | ForEach-Object { $_.FullName }
  }
  elseif (Test-Path $path -PathType Leaf -and $path -match "\\\\.pdf$") {
    # パスがPDFファイルの場合、リストに追加
    $pdfFiles += $path
  }
  else {
    Write-Host "Warning: PDFファイルまたはディレクトリではありません: $path"
  }
}

foreach ($pdfFile in $pdfFiles) {
  # PDFファイル名(拡張子なし)を取得
  $pdfName = [System.IO.Path]::GetFileNameWithoutExtension($pdfFile)
  $pdfDir = [System.IO.Path]::Combine((Get-Item $pdfFile).DirectoryName, $pdfName)

  # フォルダ作成(存在しない場合)
  if (-Not (Test-Path $pdfDir)) {
    New-Item -ItemType Directory -Path $pdfDir | Out-Null
  }

  # PDFをフォルダ内に移動
  $newPdfPath = [System.IO.Path]::Combine($pdfDir, [System.IO.Path]::GetFileName($pdfFile))
  Move-Item -Path $pdfFile -Destination $newPdfPath

  # Ghostscriptを使用してPDFを画像化
  $outputPattern = [System.IO.Path]::Combine($pdfDir, "$pdfName-%03d.png")
  $gsCommand = "-dNOPAUSE -dBATCH -sDEVICE=png16m -r300 -sOutputFile=`"$outputPattern`" `"$newPdfPath`""

  Write-Host "Converting $newPdfPath to images..."
  Start-Process -FilePath $gsPath -ArgumentList $gsCommand -NoNewWindow -Wait
}

Write-Host "処理完了!"


改良点

  1. ディレクトリが渡された場合、その中のすべてのPDFファイルを取得し処理対象に追加