问题描述
在使用iText7创建Powershell脚本之前,我使用新的iText7类创建了C#应用程序.这没有问题.
Before i created a powershell script with iText7, I created with the new iText7 class a C# application. This works without a problem.
现在,我将其转换为poweshell脚本并收到错误消息.
Now I converted it into poweshell script and get a error.
使用iTextsharp可以正常运行,但是Itextsharp是EOL,因此建议使用iText7.
with iTextsharp everything works well, but Itextsharp is EOL so iText7 is the recommended way.
我的代码有什么问题?
[string] $pdfDocuFilename = "C:\pdfTestProject1\Exports\export_" + (Get-Date -Format "yyyyMMdd_HHmmss") + ".pdf"
Add-Type -Path "C:\pdfTestProject1\dlls\BouncyCastle.Crypto.dll"
Add-Type -Path "C:\pdfTestProject1\dlls\itext.kernel.dll"
Add-Type -Path "C:\pdfTestProject1\dlls\itext.layout.dll"
Add-Type -Path "C:\pdfTestProject1\dlls\itext.io.dll"
Add-Type -Path "C:\pdfTestProject1\dlls\NLog.dll"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
例外是:
使用"1"作为参数调用".ctor"的异常:无法打开文档."在C:\ pdfTestProject1 \ printPDF.ps1:26 char:1+ $ pdf = [iText.Kernel.Pdf.PdfDocument] :: new($ pdfWriter)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:未指定:(:) [],MethodInvocationException+ FullyQualifiedErrorId:PdfException
The exception is:
Exception calling ".ctor" with "1" argument(s): "Cannot open document."At C:\pdfTestProject1\printPDF.ps1:26 char:1+ $pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : PdfException
一个人唯一想要的就是工作代码:-)
The only thing a person wantes is a working code :-)
谢谢!
推荐答案
问题是缺少一些依赖项.iText7取决于 Common.Logging
版本3.4.1(可以下载此处),而后者又依赖于相同版本3.4.1的 Common.Logging.Core
(可以下载此处).还要确保BouncyCastle依赖项是 Portable.BouncyCastle
版本1.8.1.3(可以在此处下载)).
The problem is that a couple of dependencies are missing. iText7 depends on Common.Logging
version 3.4.1 (can be downloaded here), which on turn depends on Common.Logging.Core
, same version 3.4.1 (can be downloaded here).Also make sure the BouncyCastle dependency is Portable.BouncyCastle
version 1.8.1.3 (can be downloaded here).
您不需要 NLog
依赖关系,至少iText 7不需要它.
You don't need the NLog
dependency, at least it's not needed for iText 7 to work.
话虽这么说,以下是在我的设置(iText 7.1.6,PowerShell 5.1)中运行良好的代码片段:
That being said, here is the code snipped that worked fine in my set up (iText 7.1.6, PowerShell 5.1):
[string] $pdfDocuFilename = "C:\temp\" + (Get-Date -Format "yyyyMMdd_HHmmss") + ".pdf"
Add-Type -Path "C:\temp\Common.Logging.Core.dll"
Add-Type -Path "C:\temp\Common.Logging.dll"
Add-Type -Path "C:\temp\itext.io.dll"
Add-Type -Path "C:\temp\itext.kernel.dll"
Add-Type -Path "C:\temp\BouncyCastle.Crypto.dll"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$pdf.AddNewPage()
$pdf.Close()
这篇关于如何在Powershell V5中使用Itext7,加载pdfWriter时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!