通过Powershell将PDF打印到XPS返回PDF

时间:2017-06-11 08:48:42

标签: powershell pdf xps

在这里的另一位成员的帮助下,我能够创建两个脚本:一个使用工作流将PDF转换为XPS,另一个将XPS转换为PDF。将它们组合到一个脚本中时,第一个工作流将文件转换为XPS,但是,第二个工作流似乎无法在适当的时间工作或根本不工作。在第二个工作流程的末尾有一些清理项目似乎正常工作。我可以在适当的时间以某种方式延迟第二个工作流的开始吗?提前致谢!另一个问题可以在Print PDF to XPS using Powershell找到。

# Define the directories used for converting files 
$secure_pdf_dir = Select-FolderDialog # the variable contains user folder selection
$xps_dir = "aaa"
$unsecure_pdf_dir = "bbb"
$secure_pdf_archive = "ccc"
$xps_archive = "ddd"
$unsecure_pdf_archive = "eee"

$date = $(get-date -f yyyy-MMM-dd)


# Archives old files except secure pdfs
# Archives old xps files
New-Item $xps_archive\$date -type Directory
copy-Item $xps_dir\* -Recurse $xps_archive\$date -force
Remove-Item $xps_dir\*

# Archives old unsecure pdf files
New-Item $unsecure_pdf_archive\$date -type Directory
copy-Item $unsecure_pdf_dir\* -Recurse $unsecure_pdf_archive\$date -force
Remove-Item $unsecure_pdf_dir\*


# Converts PDF to XPS
function print_files($secure_pdf_dir){
    #The purpose of this counter is to number your .xps files
    Get-ChildItem $secure_pdf_dir -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($secure_pdf_dir)
# 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

Move-Item $secure_pdf_dir\*.oxps $xps_dir

# Converts XPS to PDF
function print_xps_files($xps_dir){
    #The purpose of this counter is to number your .xps files
    Get-ChildItem $xps_dir -Filter *.oxps -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_xps_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}")
}


function press_enter($enter){
    $wshell = New-Object -ComObject wscript.shell;
    while($wshell.AppActivate('Print') -ne $true){
        $wshell.AppActivate('Print')
    }
    $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_time2{
    Param(
        $fullname
    )
    sequence{
        Start-Process -FilePath $fullname –Verb Print -PassThru
        press_enter($enter)
        enter_my_xps_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($xps_dir)
# 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

# Archives old secure pdfs
# Archives old secure pdf files
New-Item $secure_pdf_archive\$date -type Directory
copy-Item $secure_pdf_dir\* -Recurse $secure_pdf_archive\$date -force
Remove-Item $secure_pdf_dir\*

0 个答案:

没有答案