Print a PDF to a file in PowerShell

半城伤御伤魂 提交于 2021-02-19 07:38:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!