将特定 PDF 页面打印成图像的 Powershell 脚本
Powershell script to print specific PDF pages into images
我该如何更改这个 powershell 脚本
Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
至:
- 打印 PDF 的特定页面,
- 直接到图像(例如 png、jpg、tif 等),并且
- 相应地保存它们?
例如,我想将 ABC.pdf 的第 3、4、7 页打印到三个单独的文件中,分别称为 ABC_3.png、ABC_4.png 和 ABC_7.png;图像文件可以是任何格式(.png、.jpg、.tif 等)。
我打算调用 .csv 列表来获取所有参数值(例如要打印的页码、带页码的输出名称、新文件位置的文件路径等),但我不知道如何设置powershell 语法。谢谢。
更新:
我使用下面的脚本在这个任务上取得了进展,它调用了 ghostcript。它执行上面的 1-3,只是我似乎无法将我的 -dFirstPage 和 -dLastPage 设置为我的 csv 中的参数...我收到 powershell 错误:
Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
如果我用数字替换 $pg,它似乎工作正常。我将如何使用 -sNAME 来解决这个问题?
新脚本
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'
$files = Import-CSV 'C:\Data\files.csv' -Header ("FileName","Type","Map","Section","MapPg","SectionPg","Directory","PathName","LastWriteTime")
ForEach($File in $files)
{
if ($File.Map -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_Pg" + $File.MapPg + ".tif"
$param = "-sOutputFile=$tif"
$inputPDF = $File.PathName + ".pdf"
$pg = $File.MapPg
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
}
ElseIf ($File.Section -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + $File.SectionPg + ".tif"
$param = "-sOutputFile=$tif"
$inputPDF = $File.PathName + ".pdf"
$pg = $File.SectionPg
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
}
}
此处的关键是将 -dFirstPage 和 -dLastPage 移出 ghostcript 行并移入新参数(param1 和 param2)。以下作品(尽管我认为可能有更好的方法):
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'
$IPfiles = Import-CSV 'C:\Data\files.csv' -Header ("FileName","Type","Map","Section","MapPg","SectionPg","Directory","PathName","LastWriteTime")
ForEach($File in $IPfiles)
{
$pgM = $File.MapPg
$pgS = $File.SectionPg
if ($File.Map -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_MPg" + $File.MapPg + ".tif"
$param = "-sOutputFile=$tif"
$param1 = "-dFirstPage=$pgM"
$param2 = "-dLastPage=$pgM"
$inputPDF = $File.PathName + ".pdf"
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $param1 $param2 $inputPDF -c quit
}
ElseIf ($File.Section -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_SPg" + $File.SectionPg + ".tif"
$param = "-sOutputFile=$tif"
$param1 = "-dFirstPage=$pgS"
$param2 = "-dLastPage=$pgS"
$inputPDF = $File.PathName + ".pdf"
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $param1 $param2 $inputPDF -c quit
}
}
我该如何更改这个 powershell 脚本
Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
至:
- 打印 PDF 的特定页面,
- 直接到图像(例如 png、jpg、tif 等),并且
- 相应地保存它们?
例如,我想将 ABC.pdf 的第 3、4、7 页打印到三个单独的文件中,分别称为 ABC_3.png、ABC_4.png 和 ABC_7.png;图像文件可以是任何格式(.png、.jpg、.tif 等)。
我打算调用 .csv 列表来获取所有参数值(例如要打印的页码、带页码的输出名称、新文件位置的文件路径等),但我不知道如何设置powershell 语法。谢谢。
更新:
我使用下面的脚本在这个任务上取得了进展,它调用了 ghostcript。它执行上面的 1-3,只是我似乎无法将我的 -dFirstPage 和 -dLastPage 设置为我的 csv 中的参数...我收到 powershell 错误:
Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
如果我用数字替换 $pg,它似乎工作正常。我将如何使用 -sNAME 来解决这个问题?
新脚本
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'
$files = Import-CSV 'C:\Data\files.csv' -Header ("FileName","Type","Map","Section","MapPg","SectionPg","Directory","PathName","LastWriteTime")
ForEach($File in $files)
{
if ($File.Map -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_Pg" + $File.MapPg + ".tif"
$param = "-sOutputFile=$tif"
$inputPDF = $File.PathName + ".pdf"
$pg = $File.MapPg
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
}
ElseIf ($File.Section -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + $File.SectionPg + ".tif"
$param = "-sOutputFile=$tif"
$inputPDF = $File.PathName + ".pdf"
$pg = $File.SectionPg
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 -dFirstPage='$pg' -dLastPage='$pg' $inputPDF -c quit
}
}
此处的关键是将 -dFirstPage 和 -dLastPage 移出 ghostcript 行并移入新参数(param1 和 param2)。以下作品(尽管我认为可能有更好的方法):
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'
$IPfiles = Import-CSV 'C:\Data\files.csv' -Header ("FileName","Type","Map","Section","MapPg","SectionPg","Directory","PathName","LastWriteTime")
ForEach($File in $IPfiles)
{
$pgM = $File.MapPg
$pgS = $File.SectionPg
if ($File.Map -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_MPg" + $File.MapPg + ".tif"
$param = "-sOutputFile=$tif"
$param1 = "-dFirstPage=$pgM"
$param2 = "-dLastPage=$pgM"
$inputPDF = $File.PathName + ".pdf"
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $param1 $param2 $inputPDF -c quit
}
ElseIf ($File.Section -eq "T" -And $File.Type -eq "pdf")
{
$tif = $File.PathName + "_SPg" + $File.SectionPg + ".tif"
$param = "-sOutputFile=$tif"
$param1 = "-dFirstPage=$pgS"
$param2 = "-dLastPage=$pgS"
$inputPDF = $File.PathName + ".pdf"
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $param1 $param2 $inputPDF -c quit
}
}