以下の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"
gswin64c.exe
) を使ってPDFを300dpiのPNG画像に変換。
ファイル名-001.png
, ファイル名-002.png
のような形式。C:\\\\Program Files\\\\gs\\\\gs10.02.0\\\\bin\\\\gswin64c.exe
に Ghostscript がある場合を想定(バージョンに応じて適宜変更)Ghostscriptのパスが異なる場合は $gsPath
の値を変更してください。
以下の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 "処理完了!"