如何在Powershell上使用iTextSharp



我是PowerShell的新手,我正试图使用iTextSharp工具编辑PDF,但我无法使其工作,以下是我迄今为止试图打开文件的内容,但它给了我一个错误

$path = "C:...Itextliba.pdf" #path to my pdf file
[System.Reflection.Assembly]::LoadFrom("C:...Itextlibitextsharp.dll") #path to my itextsharp.dll
[System.Reflection.Assembly]::LoadFrom("C:...ItextlibBouncyCastle.Crypto.dll") #path to my BouncyCastle.Crypto.dll
New-Object iTextSharp.text.pdf.PdfReader ("$path")

错误是:

新对象:调用"的异常;。ctor";与论点";1〃:"重建失败:字典键Virtual不是名称。在文件指针2516处;原始消息:字典关键字Virtual不是名称。在文件指针2516"处;

我希望有人能帮我,提前谢谢

您收到的错误消息并非iTextSharp独有。

继续我的评论。

https://github.com/itext/itextsharp

请注意:iTextSharp是EOL,已被iText7取代。只会添加安全修复程序我们强烈建议客户在新项目中使用iText 7,并考虑将现有项目从iTextSharp转移到iText 7以受益于许多改进,例如:

在SO上有很多关于通过PowerShell的iTestSharp和iText7用例的帖子。

https://stackoverflow.com/search?q=powershell+itextsharp

https://stackoverflow.com/search?q=powershell+itext7

https://stackoverflow.com/search?q=powershell+使用+itextsharp

https://stackoverflow.com/search?q=powershell+使用+itext7

SO示例

Powershell和iTextsharp将多个图像添加到PDF

## Set various paths
$iTextSharpFilePath = "D:DLLsitextsharp.dll"
$imageFolderPath    = "D:images"
$pdfFilePath        = "D:temp.pdf"
## Load iTextSharp and System.Drawing
[System.Reflection.Assembly]::LoadFrom($iTextSharpFilePath)
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

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

问题是缺少几个依赖项。iText7依赖于Common.Logging版本3.4.1[可在此处下载],而Common.Loggin.Core又依赖于相同版本3.4.1[可以在此处下载(。此外,请确保BouncyCastle依赖项是Portable.BouncyCastle1.8.1.3版本(可以在此处下载(。

您不需要NLog依赖项,至少iText7不需要它。

话虽如此,以下是在我的设置中运行良好的代码片段(iText 7.1.6,PowerShell 5.1(:

[string] $pdfDocuFilename = "C:temp" + (Get-Date -Format "yyyyMMdd_HHmmss") + ".pdf"
Add-Type -Path "C:tempCommon.Logging.Core.dll"
Add-Type -Path "C:tempCommon.Logging.dll"
Add-Type -Path "C:tempitext.io.dll"
Add-Type -Path "C:tempitext.kernel.dll"
Add-Type -Path "C:tempBouncyCastle.Crypto.dll"

$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$pdf.AddNewPage()
$pdf.Close()

最新更新