Reputation: 1709
How I could I change this powershell script
Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
to:
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.).
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.
UPDATE:
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
if I replace $pg with a number it seems to work fine. How would I use -sNAME to fix this?
new 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
}
}
Upvotes: 3
Views: 8532
Reputation: 1709
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
}
}
Upvotes: 5