iText alignment of Acrofield [C#]

756 Views Asked by At

I got a PDF file with some Acrofields. I'd like the text of one textfield to be centered.

I tried:

var centeredField = stamper.AcroFields.GetFieldItem(fieldname);
centeredField.GetMerged(0).Put(PdfName.Q, new PdfNumber(PdfFormField.Q_CENTER));

Which works for text that is set in the code afterwards (with SetField). But as soon as I open the PDF and edit the text of the field, it get's aligned left again...

Any ideas where my code's wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

After some reading and playing around I finally managed to get a working solution:

var centeredField = stamper.AcroFields.GetFieldItem(fieldname);
centeredField.GetWidget(0).Put(PdfName.Q, new PdfNumber(PdfFormField.Q_CENTER));

The difference is the "GetWidget" instead of GetMerged.