【问题标题】:Print pdf files on different printers depending on their content根据内容在不同的打印机上打印 pdf 文件
【发布时间】:2021-03-22 22:59:45
【问题描述】:

我想在不同的打印机上打印 .pdf 文件 - 取决于它们的内容。 如何检查文件中是否存在特定的单个单词? 到目前为止,为了通过文件夹的内容排队,我已经构建了以下内容:

Unblock-File -Path S:\test\itextsharp.dll
Add-Type -Path S:\test\itextsharp.dll
$files = Get-ChildItem S:\test\*.pdf
$adobe='C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe'
foreach ($file in $files) {
  $reader = [iTextSharp.text.pdf.parser.PdfTextExtractor]
  $Extract = $reader::GetTextFromPage($File.FullName,1)
  if ($Extract -Contains 'Lieferschein') {
    Write-Host -ForegroundColor Yellow "Lieferschein"
    $printername='XX1'
    $drivername='XX1'
    $portname='192.168.X.41'
  } else {
    Write-Host -ForegroundColor Yellow "Etikett"
    $printername='XX2'
    $drivername='XX2'
    $portname='192.168.X.42'
  }
  $arglist = '/S /T "' + $file.FullName + '" "' + $printername + '" "' + $drivername + " " + $portname
  start-process $adobe -argumentlist $arglist -wait
  Start-Sleep -Seconds 15
  Remove-Item $file.FullName
}

现在我遇到了两个问题:

1stAdd-Type -Path itextsharp.dll 给了我一个错误。

Add-Type: One or more types in the assembly cannot be loaded. Get the LoaderExceptions property for more information. In line: 2 character: 1

我了解到这可能是由于文件被阻止所致。但是,属性中没有关于此的信息。 Unblock-File 命令和 start 不会改变/解决任何问题。

使用$error[0].exception.loaderexceptions[0] 后,我得到BouncyCastle.Crypto, Version=1.8.6.0 丢失的信息。 很遗憾,我还没有找到任何来源。

2ndif ($Extract -Contains 'Lieferschein') 会按我的意愿工作吗? Add-Type 加载成功后会检查短语吗?

或者:也有可能使它取决于内容的格式。例如,一种类型的文件具有 DIN A4 的大小。另一个比那个小。如果有更简单的方法来检查,你也会让我很高兴。

提前谢谢你!

【问题讨论】:

  • 错误是什么? itextsharp.dll 是 .Net 程序集吗?
  • 我已将新获得的信息添加到问题中。
  • 仅供参考,根据iTextSharp 的所有者,它不再受支持。根据链接 --- iTextSharp 已停产,并已被 iText 7 取代。只会添加安全修复程序 --- 我们强烈建议客户将 iText 7 用于新项目,并考虑将现有项目从 iTextSharp 移动到 iText 7受益于许多改进
  • 知道 iTextSharp 是一个相当定期的问答。从上面的SO搜索框看---stackoverflow.com/search?q=powershell+itextsharp
  • 我的意思是,真的,为什么要为可以在 Python 中轻松完成的事情如此努力。搜索pypdf2库。

标签: powershell pdf printing itext extract


【解决方案1】:

使用 Powershell 和 iTextSharp.dll 在 pdf 中搜索关键字。这是很常见的事情。然后,您只需使用条件逻辑发送到您选择的任何打印机。 所以,应该这样做。

Add-Type -Path 'C:\path_to_dll\itextsharp.dll'

$pdfs     = Get-ChildItem 'C:\path_to_pdfs' -Filter '*.pdf'
$export   = 'D:\Temp\PdfExport.csv'
$results  = @()
$keywords = @('Keyword1')

foreach ($pdf in $pdfs)
{
    "processing - $($pdf.FullName)"
    $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $pdf.FullName

    for ($page = 1; $page -le $reader.NumberOfPages; $page++)
    {
        $pageText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page).Split([char]0x000A)

        foreach ($keyword in $keywords)
        {
            if ($pageText -match $keyword)
            {
                $response = @{
                    keyword = $keyword
                    file    = $pdf.FullName
                    page    = $page
                }
                $results += New-Object PSObject -Property $response
            }
        }
    }

    $reader.Close()
}

"`ndone"

$results | 
Export-Csv $export -NoTypeInformation

更新

根据您的评论,关于您的错误。

同样,iTextSharp 是一个遗留问题,您确实需要迁移到 iText7。

不过,这不是 PowerShell 代码问题。这是一个 iTextSharp.dll 缺少的依赖项。即使使用 iText7,您也需要确保您的机器上有所有依赖项并正确加载。

如本 SO Q&A 中所述:

How to use Itext7 in powershell V5, Exception when loading pdfWriter

【讨论】:

  • 谢谢。我可以出于我的目的对其进行编辑。但是,正如我在问题中所写的那样,加载 itextsharp.dll 时出现错误怎么办?
【解决方案2】:

第一

nuget.org 上找到正确的版本(1.8.6)后,Add-Type 命令可以完美运行。正如预期的那样,我什至不需要unblock 命令,因为它没有在属性中标记为被阻止的文件。现在脚本开始于:

Add-Type -Path 'c:\BouncyCastle.Crypto.dll'
Add-Type -Path 'c:\itextsharp.dll'

第二次

关于检查队列:我只需要在我的if 子句中将-contains 替换为-match

if ($Extract -Contains 'Lieferschein')

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 2013-02-19
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多