0
votes

I am in the early stages of moving from iTextSharp to iText7, but am failing early. This is the code snippet:

$lib="C:\bob\pshell\iText7\itext7.7.1.1\lib\net40\itext.kernel.dll"
[System.Reflection.Assembly]::LoadFrom($lib)
$reader=  New-Object itext.kernel.pdf.PdfReader   "<valid pdf>"
$PDFdocument = New-Object itext.kernel.pdf.PdfDocument($reader)

This produces an error:

New-Object : Exception calling ".ctor" with "1" argument(s): "Cannot open document."

The $reader object seems valid, and returns a length and IsOpenedWithFullPermission returns $true. I have successfully used similar code to create and write to a PDF, but cannot create a PDFDocument from a PDFReader object. Am I missing something simple? Thanks

1
The error message is quite clear: you are missing an argument in the constructor of the PdfDocument class. It's as if you didn't read the documentation. You want to read a document in order to create another document, so where is the PdfWriter? You have to pass a PdfWriter instance to the constructor of the PdfDocument. - Bruno Lowagie
Thanks for the reply Bruno. I did check the declared constructors for PDFDocument, and the first is Void .ctor(iText.Kernel.Pdf.PdfReader), and the documentation at link suggests this is the constructor to use to 'Open PDF document in reading mode.'. In fact, I do only want to read the document so that I can extract the form fields. Thanks - Bob Humber

1 Answers

1
votes

Be sure you're also loading the correct version of BouncyCastle.Crypto (1.8.1.0 currently). I had the newer version loaded and it was barfing with the same generic error. Only figured it out by looking at the InnerException.