title: Underline form field value using itextsharp
body: <p>I have an application that uses itextsharp to fill PDF form fields.</p>
  
  <p>I got new requirement from the customer to allow underlining the fields values.</p>
  
  <p>I have read many posts including answers to questions in this site but I could't figure out a way to do it.</p>
  
  <p>Current my code does the following:</p>
  
  <ol>
  <li>Creates a PDFStamper instance</li>
  <li>Get the form fields using stamper.AcroFields property</li>
  <li>Set the field value using the AcroFields.SetFieldRichValue() method.</li>
  </ol>
  
  <p>But when I am opening the PDF the field is empty.</p>
  
  <p>I verified that the field is set as rich text in the PDF itself.</p>
  
  <p>Any idea what I am doing wrong ?</p>
  
  <p>Here is a snnipest of my code:</p>
  
  <pre><code>                FileStream stream = File.Open(targetFile, FileMode.Create);                                     
                  var pdfStamper = new PdfStamper(new PdfReader(sourceFile), stream);                     
  
                  // Iterate the fields in the PDF
                  foreach (var fieldName in pdfStamper.AcroFields.Fields.Keys)
                  {                       
                      // Get the field value of the current field
                      var fieldValue = "&lt;?xml version=\"1.0\"?&gt;&lt;body xmlns=\"http://www.w3.org/1999/xtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:contentType=\"text/html\" xfa:APIVersion=\"Acrobat:8.0.0\" xfa:spec=\"2.4\"&gt;&lt;p style=\"text-align:left\"&gt;&lt;b&gt;&lt;i&gt;Here is some bold italic text&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;&lt;p style= \"font-size:16pt\"&gt;This text uses default text state parameters but changes the font size to 16.&lt;/p&gt;&lt;/body&gt;"
  
                      // Set the field value
                      if (String.IsNullOrEmpty(fieldValue) == false)
                      {
                          pdfStamper.AcroFields.SetFieldRichValue(key, fieldValue);
                      }
                  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I have revised my code based on Mark Storer's post to a question (http://stackoverflow.com/questions/1454701/adding-rich-text-to-an-acrofield-in-itextsharp). The new code is:</p>
  
  <pre><code>        // Create reader to read the source file
          var reader = new PdfReader(sourceFile);
  
          // Create a stream for the generated file
          var stream = File.Open(targetFile, FileMode.Create); 
  
          // Create stamper to generate the new file
          var pdfStamper = new PdfStamper(reader, stream);
  
          // Field name and value
          var fieldName = "myfield";
          var fieldValue = "&lt;?xml version=\"1.0\"?&gt;&lt;body xfa:APIVersion=\"Acroform:2.7.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"&gt;&lt;p dir=\"ltr\" style=\"margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font-size:12pt\"&gt;&lt;b&gt;write line1 bold&lt;/b&gt;&lt;/p&lt;/body&gt;";
  
          // Output stream for the temporary file that should contain the apearance of the field
          var msOutput = new FileStream(@"d:\temp.pdf", FileMode.Create);
  
          // string reader to read the field value
          var textReader = new StringReader(fieldValue);
  
          // Create new document
          var document = new Document(pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position);
  
           // writer for the new doucment
          var writer = PdfWriter.GetInstance(document, msOutput);
  
          // Open the document                
          document.Open();
  
          // Get elements to append to the doucment
          var list = HTMLWorker.ParseToList(textReader, null);
  
          // Append elements to the doucment
          foreach (var element in list)
          {
              document.Add(element);
          }                                           
  
          // close the documnet
          document.Close();                            
  
          // Append push button that contains the generated content as its apearance
          // this approach is based on the suggestion from Mark storer that can be found in:
          // http://stackoverflow.com/questions/1454701/adding-rich-text-to-an-acrofield-in-itextsharp
          var button = new PushbuttonField(pdfStamper.Writer, pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position, fieldName + "_")
                               {
                                   Layout = PushbuttonField.LAYOUT_ICON_ONLY,
                                   BackgroundColor = null,
                                   Template = pdfStamper.Writer.GetImportedPage(new PdfReader(targetFile, 1)
                               };
              pdfStamper.AddAnnotation(button.Field, 1);                
  
          pdfStamper.FormFlattening = true;
          pdfStamper.Close();
          pdfStamper.Dispose();
  </code></pre>
  
  <p>But the problem now is that the temporary document contains no content....</p>
  
  <p>Any ideas ?</p>