Search code examples
c#itexttext-alignmentacrofields

iText alignment of Acrofield [C#]


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?


Solution

  • 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.