The code listed under 2 is very strange. You create a Paragraph
object which consists of some plain text, a font, leading info, indentation info, etc... Then you ask the Paragraph
for the plain text without the font info. Elementary logic tells you that the general StringBuilder
class is unaware of iTextSharp-specific concepts such as Chunk
, Font
and underline.
You are also assuming that an ordinary text field is capable of containing rich text. Ordinary text fields are usually defined to contain text in one single font. For instance: "Helvetica" for regular text. Or "Helvetica Bold" for bold text. "Helvetica" and "Helvetica Bold" are two different fonts (of the same family). It is important not to confuse the concept font with the concept font family.
"Underline" is not a quality of the font. Ordinary text fields can't be defined as "the value needs to be underlined".
This means that you'll need a work-around to achieve what you need. This workaround is described in my book, more specifically where I explain the MovieAds example (look here if you need the C# version).
In this example, I don't use the setField()
method because of the limitations of ordinary text fields. Instead, I get the position of the field and I add the rich text using the ColumnText
object:
AcroFields.FieldPosition f = form.GetFieldPositions(fieldname)[0];
ColumnText ct = new ColumnText(canvas);
ct.SetSimpleColumn(
f.position.Left, f.position.GetBottom(2),
f.position.GetRight(2), f.position.Top
);
ct.AddElement(para);
ct.Go();