使用 Powershell 将 PDF 打印到 XPS [英] Print PDF to XPS using Powershell

查看:87
本文介绍了使用 Powershell 将 PDF 打印到 XPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to convert a folder containing PDF files into XPS files using Powershell. Due to system restrictions, I am unable to download any third party software like iTextSharp to make this work.

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

I have been able to get Powershell to open the document and open the print window for XPS, but the name is always blank. Is it possible to have the new file name match the original file name?

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

Also, how can the process be automated so user input is not required (ie entering the file name or pressing print)? Lastly, is it possible to change the directory that it prints to?

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

推荐答案

我会这样做:

#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

干杯!

这篇关于使用 Powershell 将 PDF 打印到 XPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆