1

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

One of these fields has some text with tags. For example:

<U>This text should be underlined</>. 

I'd like that the text closed in .. has to be underlined.

How could I do that?

How could I approch it with HTMLWorker for example?

Here's the portion of code where I write my description:

for (int i = 0; i < linesDescription.Count; i++)
{
         int count = linesDescription[i].Count();
         int countTrim = linesDescription[i].Trim().Count();
         Chunk cnk = new Chunk(linesDescription[i] + GeneralPurpose.ReturnChar, TextStyle);

         if (firstOpe && i > MaxLinePerPage - 1)
               LongDescWrapped_dt_extra.Add(cnk);
         else
               LongDescWrapped_dt.Add(cnk);
}
Galma88
  • 2,398
  • 6
  • 29
  • 50
  • Can you clarify with a bit more detail, i.e. code. Are you using HTMLWriter? – Anthony Horne Feb 18 '15 at 08:57
  • No, I'm not using it. I have to print a description field that contains portions of text closed by tag .......>. I want to print that text underlined. So my question is: iTextSharp could read this text and write underlined? Or Do I have to write some custom code? – Galma88 Feb 18 '15 at 09:03
  • If it is HTML, then it would be . I have used IText and PDFSharp and what I have read, you can use the HTMLWriter to parse the HTML. and would be valid and not >. Alternatively, you could try a style for the whole element (div, p, etc.) – Anthony Horne Feb 18 '15 at 10:06
  • Dear @AnthonyHorne, you are talking about `HTMLWriter` when you actually mean to say `HTMLWorker`. You shouldn't advise people to use `HTMLWorker` because that class has been abandoned in favor of XML Worker. It is no longer supported (nor is `HtmlWriter` for that matter). Also: the OP is talking about filling out form field. In that case, your comment isn't helpful. – Bruno Lowagie Feb 18 '15 at 10:23

2 Answers2

3

Ordinary text fields do not support rich text. If you want the fields to remain interactive, you will need RichText fields. These are fields that are flagged in a way that they accept an RV value. This is explained here: Set different parts of a form field to have different fonts using iTextSharp (Note that I didn't succeed in getting this to work, but you may have better luck.)

If it is OK for you to flatten the form (i.e. remove all interactivity), please take a look at the FillWithUnderline example:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setFormFlattening(true);
    AcroFields form = stamper.getAcroFields();
    FieldPosition pos = form.getFieldPositions("Name").get(0);
    ColumnText ct = new ColumnText(stamper.getOverContent(pos.page));
    ct.setSimpleColumn(pos.position);
    ElementList elements = XMLWorkerHelper.parseToElementList("<div>Bruno <u>Lowagie</u></div>", null);
    for (Element element : elements) {
        ct.addElement(element);
    }
    ct.go();
    stamper.close();
}

In this example, we don't fill out the field, but we get the fields position (a page number and a rectangle). We then use ColumnText to add content at this position. As we are inputting HTML, we use XML Worker to parse the HTML into iText objects that we can add to the ColumnText object.

This is a Java example, but it should be easy to port this to C# if you know how to code in C# (which I don't).

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
1

You can trythis

            Chunk chunk = new Chunk("Underlined TExt", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12.0f, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE));
            Paragraph reportHeadline = new Paragraph(chunk);
            reportHeadline.SpacingBefore = 12.0f;
            pdfDoc.Add(reportHeadline);
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45