0

I have a specific problem that I cannot seem to resolve.

I am trying to create a PdfStamper with the following code.

The line marked with --> is where the exception pops up.

PdfReader pdfReader = new PdfReader(SourcePdfFileName);
byte[] arr;

using (MemoryStream ms = new MemoryStream())
{
  PdfStamper pdfStamper;
  --> pdfStamper = PdfStamper.CreateSignature(pdfReader, ms, '\0');
  arr = ms.ToArray();
}

and the other method I tried

PdfReader pdfReader = new PdfReader(SourcePdfFileName);
FileStream signedPdf = new FileStream(DestPdfFileName, FileMode.Create, FileAccess.Write);

--> PdfStamper pdfStamper = new PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');

Both methods throw a NullReferenceException.

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=itextsharp

StackTrace:
at iTextSharp.text.Version.GetInstance()

The original file exists on the path "SourcePdfFileName" and a blank pdf file is created when the file stream is opened. What am I missing ?

Edit: referring to this answer.

None of the parameters used in PdfStamper.CreateSignature() are null. The only null field here is the variable pdfStamper. I have tried initializing it with

PdfReader pdfReader = new PdfReader(SourcePdfFileName);
FileStream signedPdf = new FileStream(DestPdfFileName, FileMode.Create, FileAccess.Write);
PdfStamper pdfStamper = new PdfStamper(pdfReader,signedPdf);

which in return throws the same exception mentioned above.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
darko6977
  • 19
  • 1
  • 6
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor May 11 '22 at 10:33
  • @Fildor I have edited my question. Unfortunately no, the answer you mentioned did not help me. I have a feeling that the solution is simple but I just can't get a grip on what it is – darko6977 May 11 '22 at 10:48
  • Is PdfStamper an iText class or your own code? – Fildor May 11 '22 at 10:50
  • Its from ITextSharp 5.5 @Fildor – darko6977 May 11 '22 at 10:51
  • So, in the stacktrace, there is nor reference to your code as origin of the exception? Did you check for open issues in that library? – Fildor May 11 '22 at 10:55
  • This is what the output window says: `Exception thrown: 'System.NullReferenceException' in itextsharp.dll Object reference not set to an instance of an object.` – darko6977 May 11 '22 at 10:56
  • Hmmm, it would really surprise me if such an obvious bug existed in that library. Are there more recent versions you can try? – Fildor May 11 '22 at 11:07
  • Sounds similar to this one: https://stackoverflow.com/questions/61601795/itextsharp-issues-wherein-null-reference-exception – Fildor May 11 '22 at 11:09
  • It seems it is the exact same problem actually. Bad news is, the question is from 2020 and it has not been answered yet :( – darko6977 May 11 '22 at 11:15
  • Yes, and googling the issue hasn't lead me to anything useful, yet. – Fildor May 11 '22 at 11:16
  • 1
    I am trying to transfer to Itext7 right now as this seems as a lost cause – darko6977 May 11 '22 at 11:43
  • The error message appears to indicate that something goes wrong during initialization of the library. The `Version` class is where iText checks for version information and a license and builds the modified-by line accordingly (with or without "AGPL" in it). Do you have a license file? Have you included an iText license lib in your project? Or do you probably have different versions of iText in your project? – mkl May 11 '22 at 17:16
  • Originally I did have "multiple"(never had both installed at the same time) versions of IText in one project since I was testing Itext7 as well . When I encountered the above mentioned exception, I tried creating a new project and installing only Itextsharp 5.5 there and it still gave me an error. @mkl – darko6977 May 12 '22 at 07:15

0 Answers0