PDFSharp filling in form fields

I would like to fill in form fields in a premade PDF doc, but I’m receiving a Null Refrence error with AcroForm when running.

 string fileN4 = TextBox1.Text + " LOG.pdf";

  File.Copy(Path.Combine(textBox4.Text + "//", fileN4),
               Path.Combine(Directory.GetCurrentDirectory(), fileN4), true);

  // Open the file
  PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify);

  PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]);
        string caseName = TextBox1.Text;
  PdfString caseNamePdfStr = new PdfString(caseName);

  //set the value of this field
  currentField.Value = caseNamePdfStr;

  // Save the document...

So PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields[“<CASENUM>”]); is where the error happens. It seams that AcroForm is not even recognizing the fields.

Another option would be a find and replace text in a PDF (without using itextsharp as cannot use due to licensing).

Any help would be awesome!

Combining PDFs with PDFSharp losing form fields

I am attempting to concatenate two created PDF files to a new PDF using PDFSharp and this code (which I found here): // Open the output document PdfDocument outputDocument = new PdfDocument(); // Ite

Filling form fields with jquery

So I have a form that will be used for both inserting and editing data from MySQL. I thought that instead of writing two forms, I’d write just one, that will be used for both purposes. So when it’s fo

Accessing form elements filling fields?

I have a form which has an initial element, I am running a check using the confidence feature to check for other phrases that might match the user’s input. If there are then i am going to a menu, from

Filling the html form fields through servlets

So the question is like we manipulate the values of the form fields through servlets by using request.getParameter() can we do the other way round i.e set the form feild values fromthe servlet itself?

Filling PDF-Form-Fields with iTextSharp

I try to fill a PDF form with C#. But somehow it does not work. The problem: the fields object (in the line: fields.SetField(Name, Peter);) seems to be null. Here is my code: public static void F

Prevent browsers from auto-filling form fields

I have tried to find a way to prevent browsers (mostly FF) to prevent auto-filling of my form. I don’t mine auto complete, as they type in values it offers suggestions. But when I load a form in FF it

looking for jquery form plugin for easy filling form fields

The example ajax form <form method=post> <input type=text name=name /> <input type=text name=email /> <select name=group> <option value=1>group 1</optio

XPS form filling

Is there a way to do form filling with XPS documents in C#?

Converting windows form in C# to PDF using PdfSharp

I am trying to convert the current contents of a C# windows form to a pdf document. I am using the PDFSharp dll to do the conversion, I am not sure of how to capture the windows form and convert it t

Redirect to page, pre-filling form fields

I’m trying to open a web page (not under my control), and pre-populate some of the fields. I have two challenges that I’m having some trouble with. I (think I) need to redirect to the page itself, no


Have you tried putting the current directory in when you try to open it?


PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify);


PdfDocument document = PdfReader.Open(Path.Combine(Directory.GetCurrentDirectory(), fileN4), PdfDocumentOpenMode.Modify);

I’m pretty sure that PdfReader will need a full file path, although I only use ASPOSE for pdf creation.

I have just experienced something similar to this. The first pdf file I opened did not contain acroform data and resulted in a null exception as described above. The issue is not with the opening of the pdf but the reference to the Acroform member variable having a value of null. You can test your pdf using the following code example:

    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == DialogResult.OK)
        PdfDocument _document = null;
            _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify);
        catch(Exception ex)
            //do any cleanup and return

        if (_document != null)
            if (_document.AcroForm != null)
                MessageBox.Show("Acroform is object","SUCCEEDED");
                //pass acroform to some function for processing
                MessageBox.Show("Acroform is null","FAILED");
            MessageBox.Show("Uknown error opening document","FAILED");


I also noticed the key in this line of code should not have angle brackets


Change it to


You also need this if you are attempting to populate PDF form fields, you also need to set the NeedsAppearances element to true. Otherwise the PDF will “hide” the values on the form. Here is the VB code.

If objPdfSharpDocument.AcroForm.Elements.ContainsKey("/NeedAppearances") = False Then
    objPdfSharpDocument.AcroForm.Elements.Add("/NeedAppearances", New PdfSharp.Pdf.PdfBoolean(True))
    objPdfSharpDocument.AcroForm.Elements("/NeedAppearances") = New PdfSharp.Pdf.PdfBoolean(True)
End If

The solution to overcome the NullReferenceException is to open your pre-made PDF with Adobe Acrobat and give your form fields a default value, by changing their property-type to be something other than null.

I’ve been working on this today and I’ve managed to create a working solution. I’ve pasted my working code below. The only real differences I can see between my code and the OP’s is the following:

  • I included Marc Ferree’s code to set NeedAppearances (+1 and Many thanks!!)
  • I set the Text property of the field using a String variable, and not the Value property using a PdfString.

Hopefully this will be of use to somebody trying to do the same.

string templateDocPath = Server.MapPath("~/Documents/MyTemplate.pdf");
PdfDocument myTemplate = PdfReader.Open(templateDocPath, PdfDocumentOpenMode.Modify);
PdfAcroForm form = myTemplate.AcroForm;

if (form.Elements.ContainsKey("/NeedAppearances"))
    form.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
    form.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));

PdfTextField testField = (PdfTextField)(form.Fields["TestField"]);
testField.Text = "012345";

myTemplate.Save(Server.MapPath("~/Documents/Amended.pdf"));  // Save to new file.