Powershell脚本将特定PDF页面打印成图像

时间:2016-04-07 19:33:50

标签: powershell pdf printing ghostscript

我如何更改此PowerShell脚本

Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

为:

  1. 打印PDF的特定页面,
  2. 直接映射到图像(例如png,jpg,tif等)和
  3. 相应保存?
  4. 例如,我想将ABC.pdf的3,4,7页打印成三个单独的文件,分别名为ABC_3.png,ABC_4.png和ABC_7.png;图像文件可以是任何格式(.png,.jpg,.tif等)。

    我计划调用.csv列表来获取所有参数值(例如要打印的页码,带页码的输出名称,文件路径到新文件位置等)但我不知道如何设置PowerShell语法。谢谢。

    更新

    我已经使用下面的脚本在此任务上取得了进展,该脚本调用了一个代笔。除了我似乎无法将我的-dFirstPage和-dLastPage设置为来自我的csv的参数之外,它确实达到了1-3以上...我得到了powershell错误:

    Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
    

    如果我用一个数字替换$ pg似乎工作正常。我如何使用-sNAME来修复此问题?

    新SCRIPT

    #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
        }
    }
    

0 个答案:

没有答案