用于将特定PDF页面打印到图像中的Powershell脚本

用于将特定PDF页面打印到图像中的Powershell脚本,powershell,pdf,printing,ghostscript,Powershell,Pdf,Printing,Ghostscript,如何更改此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、.

如何更改此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语法。多谢各位

    更新

    我使用下面的脚本在这项任务上取得了进展,它调用了一个ghoscript。它执行上面的1-3操作,只是我似乎无法将my-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移出GhostScript行并移到新参数(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
        }
    }