如何使用详细选项自动打印pdf文件?

时间:2018-01-17 15:20:37

标签: c# powershell pdf printing

这是我想要解决的方案。我工作的公司每天都要为学生打印多个pdf。每个pdf的最后一页必须用蓝纸打印。我们当前的流程是手动打印pdf并将除最后一页之外的所有页面发送到一台装有白纸的打印机,然后将最后一页发送到托盘中有蓝纸的另一台打印机。这是耗时且乏味的。我创建了一个PowerShell脚本,它将获取给定文件夹中的所有pdf,然后首先将pdf分成两部分,第一部分是所有页面,但最后一页,第二页是最后一页。然后脚本将每个pdf发送到适当的打印机。

但是这些PDF是安全的,因此脚本不起作用。通常,它们在打开Adobe Reader几秒钟后会自动解密,但由于脚本会立即打印它们,因此没有时间进行解码。

我想知道:

  1. 有没有办法解决Powershell和
  2. 中的加密问题
  3. 此外,可以在自动打印时选择纸盘,以便仅使用一台打印机正确打印彩色页面。 (这将是理想的,因为页面将保持有序。目前,我们没有带两个托盘的打印机,但随着公司扩展,我们肯定会。)
  4. 据我所知,我认为#2需要C#,所以如果能够自动选择纸盘,我愿意废弃我的Powershell脚本。

    这是我当前的脚本(它不漂亮,对不起)

    # Set Up Folders
    $input = "C:\batchPrintPKs\unsplit_pdfs"
    $output_f = "C:\batchPrintPKs\split_pdfs_f"
    $output_l = "C:\batchPrintPKs\split_pdfs_l"
    
    # Load Adobe and PDFtk (Used to split PDFs)
    $adobe= 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe'
    $pdftk = "C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
    
    # Printer Names
    $printername_brother='Brother DCP-L2540DW series Printer'
    $printername_epson='Epson854235 (ET-4550 Series)'
    
    
    # Create List of Paths to Pdfs to Work With
    $files1 = Get-ChildItem “c:\batchPrintPKs\unsplit_pdfs\*.pdf”
    
    # For All PDFs in unsplit_pdfs
    foreach ($file1 in $files1){
    
        # Calculating Indexing
        $Match = 'NumberOfPages: (\d+)'
        $NumberOfPages = [regex]::match((& $pdftk $file1 dump_data),$Match).Groups[1].Value
        $SecondToLastPage = $NumberOfPages - 1 
    
        # Making PDF of pages 1 - Second to Last
        Get-Childitem -path $input -filter *.pdf -recurse | foreach {            
            & $pdftk $_.Fullname cat 1-$SecondToLastPage output $output_f\"f_"$_
            }
    
        # Making PDF of last page
        Get-Childitem -path $input -filter *.pdf -recurse | foreach {            
            & $pdftk $_.Fullname cat $NumberOfPages output $output_l\"l_"$_
            }
    
        # Removing File
        Remove-Item $file1
    }
    sleep(5)
    
    # Brother
        # Create List of Paths to Pdfs to Work With
        $files2 = Get-ChildItem “c:\batchPrintPKs\split_pdfs_f\*.pdf”
        
    
        # Print Each File to the Epson
        foreach ($file2 in $files2){
            $arglist1='/t "{0}" "{1}"' -f $file2, $printername_Brother
            Start-Process $adobe $arglist1
    
            sleep(2)
            # Removing File
            Remove-Item $file2
        }
    
    # Epson
        # Create List of Paths to Pdfs to Work With
        $files3 = Get-ChildItem “c:\batchPrintPKs\split_pdfs_l\*.pdf”
        
        # Print Each File to the Epson
        foreach ($file3 in $files3){
            $arglist2='/t "{0}" "{1}"' -f $file3, $printername_Epson
            Start-Process $adobe $arglist2
    
            sleep(2)
            # Removing File
            Remove-Item $file3
        }

4 个答案:

答案 0 :(得分:0)

您可以尝试在该方案中使用某些第三方解决方案,例如 Google云打印或类似

答案 1 :(得分:0)

我意识到这是一个比较老的问题,但是itextsharp是一个插件,您可以在powershell等内部读取pdf文件...

答案 2 :(得分:0)

  

通常,它们会在打开Adobe Reader后的几秒钟后自动解密,但是由于脚本立即将它们打印出来,因此没有时间进行解码。

您是否尝试添加睡眠命令?

Start-Sleep -Seconds 10 # for example to wait for 10 seconds!

答案 3 :(得分:0)

您可以通过COM对象使用Adobe API轻松做到这一点。

以下是打印页面范围的示例:

# path to PDF file
$PDF = 'C:\Users\Kiril\Downloads\file.pdf'
# declare a COM object for the Acrobat application
$App = New-Object -ComObject AcroExch.App
# declare a COM object for PDDoc
$PDDoc = New-Object -ComObject AcroExch.PDDoc
# open a PDF
$PDDoc.Open($PDF)
# get the number of pages in the pdf document
$NumPages = $PDDoc.GetNumPages()
# this hides the acrobat instance if it's visible
$App.Hide()
# this displays the document in the acrobat instance and creates an AVDoc COM object
$AVDoc = $PDDoc.OpenAVDoc("")
# printing particular pages range
$AVDoc.PrintPages(0, 1, $NumPages - 1, 0, 0)

在参考文档中对PrintPages对象的方法$AVDoc进行了详细描述。

请参阅Acrobat® and PDF Library API Reference