Reputation: 19
I've created a pdf template that includes interactive form fields. The PDF template consist of single line text fields as well as multi-line both. Some are aligned left and some are aligned right. I want some Multi-line fields text to be right aligned but it is showing the default left align. I've also made the changes and selected the alignment to right in the field properties. But Still no luck
I am using i-text to fill the forms fields dynamically. All alignment settings are done in the PDF but multi-line fields are not aligning to right as per my template design. See the image
And also when i preview that dynamically created PDF in chrome browser the alignment is not changing to the right.
The field which are aligned to left are working Fine. Can anyone suggest what i am missing?
Upvotes: 2
Views: 3247
Reputation: 89
You can update your itext version to the latest, then the problem can be solved.
Upvotes: 0
Reputation: 640
In Itext 7 as example to change single line field to multi-line field
PdfFormField field;
field = fields.get("personal.name");
field .setCheckType(PdfFormField.FF_MULTILINE);
Upvotes: 0
Reputation: 77528
You aren't telling us whether you want a solution in which you change the alignment manually (using a tool with a GUI) or programmatically (using iText).
If you want to change the alignment of a field manually, just open the PDF template (the form) in Adobe Acrobat, and select the option to change the AcroForm fields. Change the alignment from left to right.
If you want to change the alignment programmatically, you should tell us whether you are using iText 7 (which you should) or iText 5 (which you might).
If you are using iText 7, the alignment is set like this:
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 also How to align AcroFields? in the official iText FAQ.
If you are using iText 5, the alignment is set like this:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
AcroFields.Item item;
item = form.getFieldItem("personal.name");
item.getMerged(0).put(PdfName.Q, new PdfNumber(PdfFormField.Q_LEFT));
item = form.getFieldItem("personal.loginname");
item.getMerged(0).put(PdfName.Q, new PdfNumber(PdfFormField.Q_CENTER));
item = form.getFieldItem("personal.password");
item.getMerged(0).put(PdfName.Q, new PdfNumber(PdfFormField.Q_RIGHT));
form.setField("personal.name", "Test");
form.setField("personal.loginname", "Test");
form.setField("personal.password", "Test");
form.setField("personal.reason", "Test");
stamper.close();
reader.close();
See also How to align AcroFields? in the official iText FAQ.
As you can see, the iText 7 code is more elegant and easier to understand.
Upvotes: 1