如何在Powershell V5中使用Itext7,加载pdfWriter时出现异常

编程入门 行业动态 更新时间:2024-10-27 04:37:52
本文介绍了如何在Powershell V5中使用Itext7,加载pdfWriter时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用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时出现异常

本文发布于:2023-11-02 17:59:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   异常   如何在   Powershell   pdfWriter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!