Powershell script to print specific PDF pages into images

社会主义新天地 提交于 2019-12-02 02:08:39

The key here was to move the -dFirstPage and -dLastPage out of the ghostcript line and into new parameters (param1 and param2). The following works (although I imagine there may be better ways):

#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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!