chitgoks
chitgoks

Reputation: 310

PdfName.NM in iText TextField not Visible after calling setName()

This is how I create a TextField in iText 5.

import com.itextpdf.text.pdf.TextField;

TextField tf = new TextField(stamper.getWriter(), rect, "fieldname");
tf.setText("fieldvalue");
tf.setBackgroundColor(BaseColor.WHITE);
tf.setBorderColor(BaseColor.BLACK);
tf.getTextField().setName("name_here");
stamper.addAnnotation(tf.getTextField(), 1);

This works. However, when I check in RUPS. PdfName.NM does not exist. setName() is the correct method, right?

Upvotes: 1

Views: 168

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You are assuming that methods can be chained in iText 5, but that assumption is wrong. We introduced chained methods only in iText 7.

In iText 5, you need to split things up:

PdfFormField ff = tf.getTextField();
ff.setName("name_here");
stamper.addAnnotation(ff, 1);

The PdfFormField isn't a member-variable of the TextField class, and getTextField() isn't a real getter. When you trigger getTextField(), a new PdfFormField instance is created (iText is open source, just check for yourself if you're not sure).

iText 5 grew organically. I don't have a degree in computer sciences; I am self-taught in programming. If you look at the evolution of iText, you can see how my programming skills improved. If you want a really clean version of iText, please use iText 7.

Upvotes: 1

Related Questions