2

background of the issue: I may get different kind of templates which contains controls(Text Box, check box) some times in a paper/word document.pdf document templates.

Now i have to assign values in that provided templates, and have to save it as pdf document.

So i found some thing useful for templating called Openoffice,from there i am able to add controls and able to save as pdf template document.

Now for that template need to assign values, for that found one dll called iTextSharp.

based on the samples while i am trying to add , the count of the fields in the pdf template is showing as 0. could any body help me out to assign the values for the PDF template? Below is the code i am using for this case.

   private void ListFieldNames()
        {
            string pdfTemplate = @"D:\17.pdf";
            loadPDF(pdfTemplate);


            // title the form
            //this.Text += " - " + pdfTemplate;

            // create a new PDF reader based on the PDF template document
            PdfReader pdfReader = new PdfReader(pdfTemplate);

            // create and populate a string builder with each of the
            // field names available in the subject PDF
            StringBuilder sb = new StringBuilder();
            foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }

            // Write the string builder's content to the form's textbox
          //  userName.Text = sb.ToString();

        } 
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165

1 Answers1

3

Creating an interactive PDF form using OpenOffice and filling it out using iText (or iTextSharp) is a no-brainer. Please read Chapter 6 of my book, more specifically section 6.3.5 "Filling out a PDF form." The first part of this section is titled "Creating a form with OpenOffice."

enter image description here

It is important that you create real fields and that you give these fields a name. You then have to make sure that you export the document as a PDF form:

enter image description here

As you can see, you need to check the check box next to Create PDF.

I have reasons to believe that you didn't do this, because you say:

based on the samples while i am trying to add , the count of the fields in the pdf template is showing as 0.

You should share your PDF if you want us to check if the count of the fields in that PDF template is 0 or if you are doing something wrong.

If you are doing things correctly (that is: as described in my book), you can fill out the form like this:

PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader,
    new FileStream(newFile, FileMode.Create));
AcroFields form = stamper.AcroFields;         
form.SetField(key1, value1);      
form.SetField(key2, value2);      
form.SetField(key3, value3);
...
stamper.Close();

In this snippet key1, key2, key3,... are the values you defined for the fields when you created the form in OpenOffice, and value1, value2, value3,... are the values that you want to add to the fields.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • i was able to do this, but while creating the pdf template in Open office length of controls i am able to manage, but while filling the data in controls using iTextsharp controls size is very big, could you please correct me which is the right way to achieve this functionality? – Chaitanya Tallapragada Nov 08 '15 at 18:59
  • Define **size is very big**. Are you talking about *file size*, *font size* (you define this in the template; iText just obeys), *field size* (again: you define this). You shouldn't complain about fonts or fields being too big if you define them in such a way that they are too big. If fonts or fields aren't what you meant, please clarify *in a new question*. Don't use this comment section to post additional questions. – Bruno Lowagie Nov 08 '15 at 19:03
  • :Sure, and thanks for your valuable guidance related to the above question – Chaitanya Tallapragada Nov 08 '15 at 19:05
  • 1
    @BrunoLowagie, your information is excellent and the answer is clear, but your tone comes across as somewhat rude. For example `no-brainer` seems rather extreme, since it takes quite a bit of explanation to answer the question. I see from other questions that you have a Dutch heritage, so let me just say that people in most other cultures aren't as direct. On SO it would be better to adopt a more international tone. – Jim K Nov 10 '15 at 22:24