plopazo
plopazo

Reputation: 11

How to use iTextSharp on Powershell

I'm new in PowerShell and I'm trying to edit pdfs using the iTextSharp tool but I cant get it to work, here is what I have so far trying to open a file but it gives me an error

$path = "C:\...\Itext\lib\a.pdf" #path to my pdf file

[System.Reflection.Assembly]::LoadFrom("C:\...\Itext\lib\itextsharp.dll") #path to my itextsharp.dll
[System.Reflection.Assembly]::LoadFrom("C:\...\Itext\lib\BouncyCastle.Crypto.dll") #path to my BouncyCastle.Crypto.dll

New-Object iTextSharp.text.pdf.PdfReader ("$path")

And the error is this:

New-Object : Exception to call ".ctor" with the arguments "1": "Rebuild failed: Dictionary key Virtual is not a name. at file pointer 2516; Original message: Dictionary key Virtual is not a name. at file pointer 2516"

I hope someone can help me with this, thanks in advance

Upvotes: 0

Views: 9274

Answers (2)

K J
K J

Reputation: 11857

There are more modern PowerShell iText and other PS library modules that are easier to load than older depreciated itextsharp ones.

In 2021 and 2022 there was iText based PSWritePDF
https://www.powershellgallery.com/packages/PSWritePDF/0.0.20

I have shown how that can be used for custom PDF mergers here
Merge PDF files using CSV file list using Powershell

Also how to install and use 7.1.14 here Generate a pdf from powershell

And @paul-oneill shows dependencies for 7.2.4 in https://stackoverflow.com/a/75280860/10802527

In 2022 there was added iText7module https://www.powershellgallery.com/packages/IText7Module/1.0.34

And there are with differing ages, pedigrees and functions currently 27 in total in the gallery.
https://www.powershellgallery.com/packages

There are many recipies for integrating iText 7 due to its longevity and currently very few for more recent Apryse release variants.

In addition there are many 3rd party applications that can be used with PowerShell, however this question is focused on iText.

Thus the latest iText to be found is .net based 9.1 and should be considered as more secure/versatile.

The prior 9.0 nuget link can be found here https://www.nuget.org/packages/itext/9.0.0

Upvotes: 0

postanote
postanote

Reputation: 16106

The error message you are getting is no unique to iTextSharp.

Continuing from my comment.

https://github.com/itext/itextsharp

PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added We HIGHLY recommend customers use iText 7 for new projects, and to consider moving existing projects from iTextSharp to iText 7 to benefit from the many improvements such as:

There are many posts, right here on SO regarding iTestSharp and iText7 use cases via PowerShell.

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

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

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

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

SO Example(s)

Powershell and iTextsharp add multiple images to PDF

## Set various paths
$iTextSharpFilePath = "D:\DLLs\itextsharp.dll"
$imageFolderPath    = "D:\images"
$pdfFilePath        = "D:\temp.pdf"

## Load iTextSharp and System.Drawing
[System.Reflection.Assembly]::LoadFrom($iTextSharpFilePath)
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

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

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).

You don't need the NLog dependency, at least it's not needed for iText 7 to work.

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()

Upvotes: 0

Related Questions