【问题标题】:How to Automate Print to PDF with Windows Powershell如何使用 Windows Powershell 自动打印到 PDF
【发布时间】:2021-01-17 12:16:18
【问题描述】:

我有一个包含 n 个单词、excel 和 powerpoint 文件的文件夹,扩展名为“.Doc、.Docx、.xls、.xlsx、.ppt 等”。这些文件的内容应使用 Microsoft Print to PDF 选项转换为 PDF 而不会更改其格式,并且输出文件应自动保存到扩展名为 .pdf 的文件夹中。

我已经尝试过使用下面的脚本,它只打印 PDF 中的空白页。

请帮忙。

function ConvertTo-PDF {
    param(
        $TextDocumentPath
    )
    
    Add-Type -AssemblyName System.Drawing
    $doc = New-Object System.Drawing.Printing.PrintDocument
    $doc.DocumentName = $TextDocumentPath
    $doc.PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
    $doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
    $doc.PrinterSettings.PrintToFile = $true
    $file=[io.fileinfo]$TextDocumentPath
    $pdf= [io.path]::Combine($file.DirectoryName, $file.BaseName) + '.pdf'
    $doc.PrinterSettings.PrintFileName = $pdf
    $doc.Print()
    $doc.Dispose()
}

谢谢。

【问题讨论】:

    标签: windows powershell printtopdf


    【解决方案1】:

    见: Control output location when using Powershell Out-Printer to a File

    使用本机 Powershell cmdlet,您必须解决唯一没有 -output 参数的问题 - 但它是在那里处理的。

    Function Printto-PDF ($filepath) {
           $VerbosePreference = "Continue"
           add-type -AssemblyName microsoft.VisualBasic
           add-type -AssemblyName System.Windows.Forms
           $focus = 2000
           $FilePath = Get-Item $Filepath
           $newfile = $Filepath.basename + '.pdf'
           Start-Process $Filepath.fullname -verb Print | out-printer -name "Microsoft print to PDF"  
           start-sleep -Milliseconds $focus
           [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
           start-sleep -Milliseconds 250
           [System.Windows.Forms.SendKeys]::SendWait($newfile)
           start-sleep -Milliseconds 250
           [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
           start-sleep -Milliseconds 1500
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多