Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7879687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:57:24+00:00 2026-06-03T03:57:24+00:00

I have an application that uses itextsharp to fill PDF form fields. I got

  • 0

I have an application that uses itextsharp to fill PDF form fields.

I got new requirement from the customer to allow underlining the fields values.

I have read many posts including answers to questions in this site but I could’t figure out a way to do it.

Current my code does the following:

  1. Creates a PDFStamper instance
  2. Get the form fields using stamper.AcroFields property
  3. Set the field value using the AcroFields.SetFieldRichValue() method.

But when I am opening the PDF the field is empty.

I verified that the field is set as rich text in the PDF itself.

Any idea what I am doing wrong ?

Here is a snnipest of my 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 = "<?xml version=\"1.0\"?><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\"><p style=\"text-align:left\"><b><i>Here is some bold italic text</i></b></p><p style= \"font-size:16pt\">This text uses default text state parameters but changes the font size to 16.</p></body>"

                    // Set the field value
                    if (String.IsNullOrEmpty(fieldValue) == false)
                    {
                        pdfStamper.AcroFields.SetFieldRichValue(key, fieldValue);
                    }
                }

Edit:

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:

        // 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 = "<?xml version=\"1.0\"?><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/\"><p dir=\"ltr\" style=\"margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font-size:12pt\"><b>write line1 bold</b></p</body>";

        // 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();

But the problem now is that the temporary document contains no content….

Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:57:26+00:00Added an answer on June 3, 2026 at 3:57 am

    I managed to resolve it with little trick – I used Anchor (hyperling) element inside ColumnText element and position it above the form field. The anchor is displayed with underline by default. In order to avoid the hand marker when the user hover the mouse on the anchor, I set the “Reference” property of the anchor to null.

    Here is the code I used:

     // Create reader
     var reader = new PdfReader(sourceFilePathAndName);
    
     // Create stream for the target file
     var stream = File.Open(targetFilePathAndName, FileMode.Create); 
    
     // Create the stamper
     var pdfStamper = new PdfStamper(reader, stream);
    
     const string fieldName = "MyField";     
    
     // Get the position of the field
     var targetPosition = pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position;
    
     // Set the font
     var fontNormal = FontFactory.GetFont("Arial", 16, Font.UNDERLINE, BaseColor.BLACK);
    
     // Create the anchor
     var url = new Anchor("some default text", fontNormal) { Reference = null };
    
     // Create the element that will contain the anchor and allow to position it anywhere on the document
     var data = new ColumnText(pdfStamper.GetOverContent(1));
    
     // Add the anchor to its container
     data.SetSimpleColumn(url, targetPosition.Left, targetPosition.Bottom, targetPosition.Right, targetPosition.Top, 0, 0);
    
     // Write the content to the document
     data.Go();
    
     pdfStamper.FormFlattening = true;
     pdfStamper.Close();
     pdfStamper.Dispose();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses the new features of honeycomb for the PreferenceActivity.
I have an application that uses the bluetooth to receive some data (bytes) from
I have an application that uses pictures, audio and some text from .txt... The
We have an application that uses the window message queue to pass data from
I have web application that uses JasperReports for PDF report generation. I'd like to
I have an application that uses a generic repository pattern for data access. As
I have an application that uses richfaces 4.1 components and relies on an old,
I have an application that uses NHibernate as its ORM and sometimes it experiences
I have client application that uses WCF service to insert some data to backend
I have an application that uses the accelerometer. Sometimes, the application will launch without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.