【问题标题】:Print PDF to XPS using Powershell使用 Powershell 将 PDF 打印到 XPS
【发布时间】:2017-11-10 03:34:31
【问题描述】:

我想使用 Powershell 将包含 PDF 文件的文件夹转换为 XPS 文件。由于系统限制,我无法下载任何第三方软件,如 iTextSharp 来完成这项工作。

我已经能够让 Powershell 打开文档并打开 XPS 的打印窗口,但名称始终为空白。新文件名可以和原文件名匹配吗?

此外,该过程如何实现自动化,从而不需要用户输入(即输入文件名或按打印)?最后,是否可以更改打印到的目录?

Get-ChildItem -path $pdf_filepath -recurse -include *.pdf | ForEach-Object {Start-Process -FilePath $_.fullname -Verb Print -PassThru | %{sleep 10;$_} } 

【问题讨论】:

    标签: powershell pdf xps


    【解决方案1】:

    我会这样做:

    #Define the directory containing your .pdf files
    $mydir="$env:USERPROFILE\Desktop\New folder"
    function print_files($mydir){
        #The purpose of this counter is to number your .xps files
        Get-ChildItem $mydir -Filter *.pdf -Recurse | Foreach-Object {
            #For each .pdf file in that directory, continue
            same_time $_.FullName
        }
    }
    #The following function keeps checking for a new window called "Save Print Output As". When the window shows up, it enters the name of the file and press ENTER.
    function enter_my_names($fullname){
        $wshell = New-Object -ComObject wscript.shell;
        while($wshell.AppActivate('Save Print Output As') -ne $true){
            $wshell.AppActivate('Save Print Output As')
        }
        $basename = [io.path]::GetFileNameWithoutExtension($fullname)
        #This is where the name is actually entered
        $wshell.SendKeys("$basename")
        $wshell.SendKeys("{ENTER}")
    }
    #The following function launches simultaneously a print job on the input file and a function waiting for the print job to show up to name the file.
    workflow same_time{
        Param(
            $fullname
        )
        parallel{
            Start-Process -FilePath $fullname –Verb Print -PassThru
            enter_my_names($fullname)
        }
    }
    #MAIN PROGRAM
    #Here the script saves your current printer as default
    $defprinter = Get-WmiObject -Query "Select * from Win32_Printer Where Default=$true"
    #Queries for a XPS printer
    $printer = Get-WmiObject -Query "Select * from Win32_Printer Where Name='Microsoft XPS Document Writer'"
    #Sets the XPS printer as Default
    $printer.SetDefaultPrinter()
    #Starts the main job
    print_files($mydir)
    #Sets the old default printer back as default again
    $defprinter.SetDefaultPrinter()
    #This is a small delay to be sure everything is completed before closing Adobe Reader. You can probably shorten it a bit
    sleep 5
    #Finally, close Adobe Reader
    Get-Process "acrord32" | Stop-Process
    

    干杯!

    【讨论】:

    • print_files 函数获取目录中文件的名称并将这些名称存储在 C:\c 文件夹中以供以后使用。 enter_my_names 函数打开窗口,调用存储在 C:\c 中的名称并使用新名称打印文件。工作流调用 enter_my_names 函数以并行运行以使其更快。最后,print_files 将文件实际打印到指定目录,然后从 C:\c 中删除不必要的信息。我描述的准确吗?我遇到了后续文件尝试覆盖的问题,因为名称没有改变。
    • @MrKGado 我修改了脚本。现在肯定好多了。还对其进行了注释,以便您可以更好地理解每个部分。需要安装Adobe Reader,希望不是大问题。祝你有美好的一天
    • 这很有帮助,谢谢!如何使用 Get-ChildItem 收集原始文件名,以便打印的文件具有相同的名称?我尝试修改第 6 行和第 8 行以及其他 $counter 部分,但每次都失败了。我使用Get-ChildItem -Path $mydir -Recurse -Name 收集文件名,但我无法找到如何将它们链接到打印保存名称。有什么想法吗?
    • @MrkGado 您需要修改的部分是函数 enter_my_names。我做了相应的修改。
    • 最后两个问题。我对此进行了修改,然后将这些 xps 文件转换回 PDF(必须删除防止添加 cmets 的安全性),并且效果很好。但是,当“打印”屏幕出现时,它需要用户输入。此外,它不会保存在同一目录中。如何自动化“打印”屏幕(SendKeys?),以及如何更改目录以将其保存(与 xps 文件相同的文件夹或选择,没关系)?再次感谢您!
    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多