0

I tried to fill a acrofields in c# but for some reason, it wont work.

There is my code :

using iText.Layout;
using iText.Forms;
using iText.Kernel.Pdf;
using iText.Forms.Fields;

fileNameExisting = @"Facturation.pdf";
fileNameNew = @"newfacturation.pdf";

public class Pdfinterne {
public static void Fillfact(string fileNameExisting, string fileNameNew)
{
   
    PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(new PdfReader(fileNameExisting), new PdfWriter(fileNameNew));
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);

    form.GetField("Contrat").SetValue("12345", "12345");
    
    pdfDoc.Close();
}

The error is at the PdfAcroForm line.

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

I just dont get it. I tried with itextsharp (pdfstamper) but read that it was fixed with Itext7 so i moove to Itext7 but exacly same problem.

The Facutration.pdf file is working fine and all the acrofields can be fill.

Any one get an hint on this ?

Thanks

Edit : Modify code

  • Please show the complete stack trace, and please take some time to reformat your code - at the moment the statement declaring the `pdfDoc` variable looks like it's actually three statements at first sight. (If you provided a [mcve] with `using` directives for all the namespaces you used, that would be a much shorter line, too...) – Jon Skeet Jun 21 '21 at 16:55
  • The code you posted has a number of opportunities for null references. See duplicate for extensive advice about diagnosing and fixing such problems. Note however that there have been such exceptions reported in the iText library itself, i.e. a bug in the library. With a full stack trace, you may be able to distinguish the two possibilities so you can tell whether to debug and fix your code or report the reproducible problem to the author of the library. – Peter Duniho Jun 21 '21 at 16:59

0 Answers0