问题
I need to automatically print a PDF file to a file (need to have printer driver set all the print options like stapling, duplexing, etc) on a network folder so other employees can print the .prn file from networked printers.
After a fair bit of searching I have found that it is possible to have PowerShell print the PDF using
Start-Process -FilePath document.pdf -Verb Print
which invokes the appropriate application to print the PDF but doesn't allow me to check the "Print to file" box.
I could set the default printer's port to FILE:, but then this requires user interaction to specify the destination .prn file name.
A related question (Print to File Programatically using Adobe Acrobat) seems to show it is possible with C# but I have not been able to find anything for PowerShell. It would be ideal if this is possible with PowerShell (I don't know C#) or am I stuck with programmatically interacting with the "Save to File" dialog box?
Grateful for any hint(s).
回答1:
This should help you get started:
$PrintDocument = New-Object System.Drawing.Printing.PrintDocument
$PrintDocument.DocumentName = "c:\temp\yourPdf.pdf"
$printDocument.PrinterSettings.PrintToFile = $true
$printDocument.PrinterSettings.PrintFileName = 'c:\temp\test.txt'
$PrintDocument.Print()
if you look at the $printDocument.PrinterSettings there are quite a few properties:
($PrintDocument.PrinterSettings | gm -MemberType Property ).Name -join ','
CanDuplex,Collate,Copies,DefaultPageSettings,Duplex,FromPage,IsDefaultPrinter,IsPlotter,IsValid,LandscapeAngle,MaximumCopies,MaximumPage,MinimumPage,PaperSizes,PaperSources,PrinterName,PrinterResolutions,PrintFileName,PrintRange,PrintToFile,SupportsColor,ToPage
来源:https://stackoverflow.com/questions/52785842/print-a-pdf-to-a-file-in-powershell