Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell将PDF打印到XPS_Powershell_Pdf_Xps - Fatal编程技术网

使用Powershell将PDF打印到XPS

使用Powershell将PDF打印到XPS,powershell,pdf,xps,Powershell,Pdf,Xps,我想使用Powershell将包含PDF文件的文件夹转换为XPS文件。由于系统限制,我无法下载任何像iTextSharp这样的第三方软件来实现此功能 我已经能够让Powershell打开文档并打开XPS的打印窗口,但名称始终为空。是否可以使新文件名与原始文件名匹配 此外,该过程如何实现自动化,从而不需要用户输入(即输入文件名或按print)?最后,是否可以更改打印到的目录 Get-ChildItem -path $pdf_filepath -recurse -include *.pdf | Fo

我想使用Powershell将包含PDF文件的文件夹转换为XPS文件。由于系统限制,我无法下载任何像iTextSharp这样的第三方软件来实现此功能

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

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

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

干杯

print_files函数获取目录中文件的名称,并将这些名称存储在C:\C文件夹中,以供以后使用。enter_my_names函数打开窗口,调用存储在C:\C中的名称,并用新名称打印文件。工作流调用enter_my_names函数并行运行,以加快运行速度。最后,print_文件实际上将文件打印到指定的目录,然后从C:\C中删除不必要的信息。我描述得准确吗?由于名称没有更改,我遇到了后续文件试图覆盖的问题。@MrKGado我修改了脚本。现在肯定好多了。还对其进行了注释,以便您更好地理解每个部分。它需要安装AdobeReader,希望这不是一个大问题。祝你度过愉快的一天,这对你很有帮助,谢谢!如何使用Get ChildItem收集原始文件名,以便打印的文件具有相同的名称?我试图修改第6行和第8行以及其他$counter部分,但每次都失败了。我使用
getchilditem-Path$mydir-Recurse-Name
收集文件名,但我无法找到如何将它们链接到打印保存名。有什么想法吗?@MrkGado您需要修改的部分是“输入我的名字”功能。我做了相应的修改。最后两个问题。我对此进行了修改,然后将这些xps文件转换回PDF(这是删除阻止添加注释的安全性所必需的),效果非常好。但是,当“打印”屏幕出现时,它需要用户输入。而且,它不会保存在同一个目录中。如何使“打印”屏幕(SendKeys?)自动化,以及如何更改目录以将其保存在(与xps文件相同的文件夹或选择,无所谓)?再次感谢你!