Powershell脚本可将特定的PDF页面打印到图像中 [英] Powershell script to print specific PDF pages into images
问题描述
如何更改此powershell脚本
How I could I change this powershell script
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等).
For example, I want to print pages 3,4,7 of ABC.pdf into three separate files called ABC_3.png, ABC_4.png, and ABC_7.png; the image file can be any format (.png, .jpg, .tif, etc.).
我计划调用.csv列表以获取所有参数值(例如要打印的页码,带有页码的输出名称,新文件位置的文件路径等),但是我不知道如何设置powershell语法.谢谢.
I plan to call on a .csv list to get all the parameter values (e.g. page number to print, output name with page number, filepath to new file location, etc) but I don't know how to set up the powershell syntax. Thank you.
更新:
我已经通过下面的脚本在此任务上取得了进展,该脚本调用了一个幽灵cri.它执行上面的1-3,除了我似乎无法将csv中的-dFirstPage和-dLastPage设置为参数...我得到了powershell错误:
I've made progress on this task with the script below, which calls on a ghostcript. It does 1-3 above except that I can't seem to set my -dFirstPage and -dLastPage to a parameter from my csv... I get the powershell error:
Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
如果我将$ pg替换为一个数字,它似乎可以正常工作.我将如何使用-sNAME来解决此问题?
if I replace $pg with a number it seems to work fine. How would I use -sNAME to fix this?
新脚本
#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)中.以下作品(尽管我想可能会有更好的方法):
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
}
}
这篇关于Powershell脚本可将特定的PDF页面打印到图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!