【问题标题】:How to automatically print pdfs with detailed options?如何自动打印带有详细选项的 pdf?
【发布时间】:2018-06-26 12:43:51
【问题描述】:

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

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

我想知道:

  1. 有没有办法解决 Powershell 和
  2. 中的加密问题
  3. 此外,能够在自动打印时选择纸盘,以便仅使用一台打印机正确打印彩色页面。 (这将是理想的,因为页面将保持有序。目前,我们没有带两个托盘的打印机,但随着公司的扩张,我们肯定会这样做。)

据我所知,我相信 #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
    }

【问题讨论】:

  • 在自动化处理任何语言的 pdf 文件时,您总是会遇到困难。
  • 您是否考虑过/尝试过等待解密,例如Start-Sleep -s 3?
  • 加密问题首先出现在我使用PDFtk分割pdf的时候,所以Adobe Reader暂时还没有涉及。所以我不相信这会有所帮助。
  • 我很困惑,您在帖子中写道“由于脚本立即打印它们,因此没有时间解码”,但如果问题在于 PDFtk 打开文件,您为什么会认为时间是问题吗?那么问题不是“我如何使用powershell解密PDF文件”,然后它可以与PDFtk一起正常工作吗?
  • 您的困惑是可以理解的。起初,我以为是因为没有时间解密,但知道我意识到 PDFtk 无法拆分它,因为它是安全的。我认为 Adob​​e Reader 会通过 JavaScript 自动解密它。我不清楚到底发生了什么。我所知道的是,如果我用 Adob​​e Reader 打开文件,它需要几秒钟才能查看和打印。

标签: c# powershell pdf printing


【解决方案1】:

您可以通过 COM 对象使用 Adob​​e 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)

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

请看Acrobat® and PDF Library API Reference

【讨论】:

    【解决方案2】:

    您可以尝试在这种情况下使用一些第 3 方解决方案,例如 Google 云打印 或类似的

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        通常它们会在打开 Adob​​e Reader 几秒后自动解密,但由于脚本会立即打印它们,因此没有时间进行解码。

        你试过添加睡眠命令吗?

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

        【讨论】:

          猜你喜欢
          • 2012-01-01
          • 2019-09-10
          • 1970-01-01
          • 1970-01-01
          • 2011-07-07
          • 2017-02-03
          • 2016-03-25
          • 1970-01-01
          • 2011-08-01
          相关资源
          最近更新 更多