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

10
以下是需要翻译的内容:

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

但是这些PDF受到保护,因此脚本无法运行。通常在打开Adobe Reader几秒钟后它们会自动解密,但由于脚本立即打印它们,因此没有时间进行解码。

我想知道:

  1. 是否有一种方法可以在Powershell中解决加密问题?
  2. 此外,是否能够选择托盘来自动打印正确的彩色页面并只使用一个打印机?(这将是理想的,因为页面仍将按顺序排列。目前,我们没有两个托盘的打印机,但随着公司的扩张,我们肯定会有。)

据我所知,我认为问题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-ChildItemc:\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-ChildItemc:\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-ChildItemc:\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
    }


6
无论使用哪种语言,自动化处理PDF格式的工作都是困难的。 - Maximilian Burszley
1
你有没有考虑/尝试过使用硬等待来让它解密,比如Start-Sleep -s 3 - Scotty H
1
我感到困惑,你在你的帖子中写道:“由于脚本立即打印它们,所以没有时间进行解码”,但如果问题是PDFtk打开文件,你为什么会认为时间是问题呢?那么问题不应该是“如何使用PowerShell解密PDF文件”,这样就可以很好地与PDFtk配合使用了吗? - Jacob Colvin
你的困惑是可以理解的。起初,我以为是因为没有时间进行解密,但现在我意识到 PDFtk 无法拆分它,因为它受到了保护。我认为 Adobe Reader 通过 JavaScript 自动解密它。我不清楚具体发生了什么。我只知道如果我用 Adobe Reader 打开文件,需要几秒钟才能查看和打印它。 - Kurt Hoelsema
@KurtHoelsema 如果您能提供密码,PDFtk可以在执行其他操作之前解密受密码保护的PDF文件 - 您尝试过吗?至少在Linux上,您可以使用pdftk ... input_pw password ... - Jacob Colvin
显示剩余5条评论
4个回答

1
你可以通过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)
< p > $AVDoc对象的PrintPages方法在参考文档中有很好的描述。

请参见Acrobat® and PDF Library API Reference


0

我知道这是一个比较老的问题,但是itextsharp是一个插件,可以在PowerShell等程序中读取PDF文件...


0
通常在打开Adobe Reader几秒钟后,它们会自动解密,但由于脚本立即打印它们,所以没有时间进行解码。
你尝试过添加睡眠命令吗?
Start-Sleep -Seconds 10 # for example to wait for 10 seconds!

0

你可以尝试在这种情况下使用一些第三方解决方案,例如Google Cloud Print或类似方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接