【发布时间】: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
}
现在我遇到了两个问题:
1st:Add-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 丢失的信息。 很遗憾,我还没有找到任何来源。
2nd:if ($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