1

Given a PDF form with fields already specified, I want to be able to put something like this in one of the form fields:

Rodrigo, you are NOT the father!

Using iTextSharp, I can accomplish this (albeit a bit painstakingly) using Rich Text values and passing in XML representing the content with the appropriate bold markup, as per this answer.

However, according to that same answer (and my firsthand experience), you cannot lock+flatten the form (to prevent further editing) after you have done this. Unfortunately, that is also necessary in this case.

The author of the previously-mentioned answer mentions that this can possibly be worked around by "[having Javascript] that resets the field value when the form its opened to force Acrobat/Reader to build the appearance[s] for you."

How do you add Javascript code to a PDF (preferably via iTextSharp itself, although we may be able to do it on the back-end in the file itself) such that it executes when the form is opened to force the PDF reader to build appearances for you?

Moreover, will this allow us to not have to say GenerateAppearances = false and therefore allow us to lock and flatten the form?

If not, is there something other than iTextSharp that will allow us to make certain words in a form field bold but also lock and flatten the form to prevent future editing once we're done?

Community
  • 1
  • 1

1 Answers1

1

As I found out there is a way to accomplish this goal, though not via any particularly elegant means (that I know of).

The way you do it is by using iTextSharp to write a text field directly over the existing form field.

The main caveat here is that the pdf ceases to become a form, basically, so if you ever need to read the contents of the form fields, you're hosed.

On the other hand, if the form fields are basically being used as a simple guide to help tell iTextSharp where to put text on the pdf (and exist for output only), then this might just work.

using (FileStream filestream = new FileStream(outputpath, FileMode.CreateNew, FileAccess.Write))
{
    var stamper = new PdfStamper(reader, filestream);

    var acroFields = stamper.AcroFields;

    string fieldName = "Field Name";
    var fieldPositions = acroFields.GetFieldPositions(fieldName);

    var helveticaBold = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14);
    var helveticaRegular = FontFactory.GetFont(FontFactory.HELVETICA, 14);

    List<Chunk> chunks = new List<Chunk>();

    chunks.Add(new Chunk("Rodrigo, you are ", helveticaRegular));
    chunks.Add(new Chunk("NOT", helveticaBold));
    chunks.Add(new Chunk(" the father!", helveticaRegular));

    Phrase currentPhrase = new Phrase();

    foreach (Chunk chunk in chunks)
    {
        currentPhrase.Add(chunk);
    }

    foreach (var currentPosition in fieldPositions)
    {
        PdfContentByte currentCanvas = stamper.GetOverContent(currentPosition.page);

        ColumnText currentColumnText = new ColumnText(currentCanvas);

        currentColumnText.SetSimpleColumn(currentPhrase, currentPosition.position.Left,
                                          currentPosition.position.Bottom, currentPosition.position.Right,
                                          currentPosition.position.Top, 13, Element.ALIGN_LEFT);
        currentColumnText.Go();
    }

    stamper.FormFlattening = true;
    stamper.Close();
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110