1

I'm attempting to use ITextSharp (version 5.5.13.3) in C# console app to open a pdf file for filling out the form fields, but am stuck as soon as I attempt to instantiate the PdfStamper with a stream. I've tried about a dozen or so pdf fillable files (not locked, nor encrypted) and verified permissions (set to "EveryOne"), but can't figure out what is actually null! The stream is valid from what I can tell and the PdfReader is, indeed, opening the file. This is my first attempt at doing this, but can't find a genuine working example (I copied this from a website as an example, and it doesn't work). Could it be the stream is of the wrong type? The error message is:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Since both the reader (which I can debug and see all the properties of the pdf file and form fields, no problem) and the stream are supposedly instantiated, what could this be?

        using (MemoryStream ms = new MemoryStream())
        {              

            PdfReader pdfReader = new PdfReader("D:/TestData/Direct Deposit Signup Form.pdf");

            using (PdfStamper pdfStamper = new PdfStamper(pdfReader, ms, '\0', true))
            { ... }

The error appears on the

using pdfStamper

line. Doesn't seem to matter where the files are nor the security settings.

The full error (though, as usual, it's so cryptic who knows what is really going on):

    System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=itextsharp
  StackTrace:
   at iTextSharp.text.Version.GetInstance()

I haven't used this tool before, but would love to see a working example - perhaps the MemoryStream is the problem. Not sure where to start looking. Maybe I should download their source into another project and debug? Any ideas?

MC9000
  • 2,076
  • 7
  • 45
  • 80
  • there is nothing wrong with the code sample you provided. Could it be related to file access/permissions or license stuff? – Slack Groverglow Aug 29 '22 at 00:35
  • That's what I first thought. I'm using VS2022 - maybe I need to change permissions there. I'll give it a try – MC9000 Aug 29 '22 at 00:44
  • Tried setting VS to admin mode and tried a few more PDFs - edited and saved within Edge - so it's still editable. File permissions set to Everyone with Full Control. – MC9000 Aug 29 '22 at 00:54
  • I also added Guest with Full Control. No joy. Maybe every PDF I have has a hidden security setting? Looking for more to test with. – MC9000 Aug 29 '22 at 01:00
  • 1
    Ok, it looks like all my PDFs for testing have no headers (corrupt, though they work inside all my PDF programs and in Edge). All of the PDFs on this site have no header info: https://www.sampleforms.com/fillable-forms.html So I found another site: https://www.aloaha.com/wp-content/uploads/2016/07/SampleForm-1.pdf but the same problem in this topic occurs. Can anyone suggest a library that works? IText 7 appears to have the same bugs, so it's a no go as well. Not sure how anyone is getting these libraries to work with PDFs created in the last 5 or so years. – MC9000 Aug 29 '22 at 03:33

2 Answers2

2

I'm using VS 2022. In Debug Options ' turn on "Just My Code" ' worked for me. It was mentioned here iTextSharp GetInstance - NullReferenceException error

dr9
  • 51
  • 1
  • 7
0

Found one solution: Use PDFSharp! It just works - here's an example

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace WPFfiller3
{
    public class Program
    {
        static void Main(string[] args)
        {
            FillPdfFields(@"D:\TestData\SampleForm-1.pdf", @"D:\TestData\SampleForm-1-FILLED.pdf");
        }

        private static void FillPdfFields(string SourcePDF, string DestinationPDF)
        {
            using (PdfDocument pd = PdfReader.Open(SourcePDF, PdfDocumentOpenMode.Modify))
            {
                int TotalFields = pd.AcroForm.Fields.Count;
                for (int i = 0; i < TotalFields; i++)
                {
                    pd.AcroForm.Fields[i].Value = new PdfString("test123");
                }
                pd.Save(DestinationPDF);
            }
        }
    }
}

Test PDF file used is here SampleForm-1.pdf

MC9000
  • 2,076
  • 7
  • 45
  • 80