How to align AcroFields?

Tags: formsfield flagfield propertiesalignmentiText 7

I'm using iText to populate the data to PDF templates, which are created in OpenOffice. The form is populating fine; I'm getting proper PDF, but I want to change the alignment of some fields. I am using the following code.

fields.setFieldProperty(fieldName, "fflags", PdfFormField.Q_LEFT, null);
Posted on StackOverflow on Jun 19, 2014 by Rajesh

In iText 7 to set the correct alignment of text you should use setJustification() method:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getFormFields();
PdfFormField field;
field = fields.get("personal.name");
field.setJustification(PdfFormField.ALIGN_LEFT);
field.setValue("Test");
field = fields.get("personal.loginname");
field.setJustification(PdfFormField.ALIGN_CENTER);
field.setValue("Test");
field = fields.get("personal.password");
field.setJustification(PdfFormField.ALIGN_RIGHT);
field.setValue("Test");
field = fields.get("personal.reason");
field.setValue("Test");
pdfDoc.close();

See the AlignField for the full example.

Click this link if you want to see how to answer this question in iText 5.