Fill out a PDF form with Itextsharp

I am trying to fill out a form using ITextsharp and try the following code to get all fields in pdf:

  string pdfTemplate = @"c:\Temp\questionnaire.pdf";


            PdfReader pdfReader = new PdfReader(pdfTemplate);

            StringBuilder sb = new StringBuilder();
            foreach (var de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }

But the foreach loop is always zero. I need to do something for the file since I tried the example from here and it works fine ... this is a pdf example that I am trying to fill

any ideas?

Edit ::

Null Error on PDF Fields

+5
source share
2 answers

As it turned out, the β€œform” of the PDF for filling was not actually a form (in terms of PDF) at all. So you have two options:

  • You add text to the content of the page directly, using rigid or customized "field" positions and sizes, as described in @ tschmit007 in the comments to his answer.

  • PDF PDF , PDF, .

, - , , . Adobe Acrobat, iText (Sharp). 8 iText - 2nd Edition Java . Net.

PDF. PdfStamper, PdfWriter, stamper.getWriter() Java stamper.Writer #. writer.addAnnotation(field) stamper.addAnnotation(field, page).

+3

:

using (FileStream outFile = new FileStream("result.pdf", FileMode.Create)) {
    PdfReader pdfReader = new PdfReader("file.pdf");
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outFile);
    AcroFields fields = pdfStamper.AcroFields;
    //rest of the code here
    //fields.SetField("nΒ°1", "value");
    //...
    pdfStamper.Close();
    pdfReader.Close();
}
+3

All Articles