How to move an AcroForm field?

Tags: formsfield positionmove fieldInspect fieldsiText 5

I have a process that inserts a table of content into an existing Acroform, and I am able to track where I need to start that content. However, I have existing Acrofields below that point that will need to be moved up or down, based on the height of the tables I insert. With that, how can I change the position of an Acrofield?

Posted on StackOverflow on Nov 7, 2013 by cro717

First some info about fields and their representation on one or more pages. A PDF form can contain a number of fields. Fields have unique names in the sense that one specific field with one specific name has one and one value. Fields are defined using a field dictionary.

Each field can have zero, one or more representations in the document. These visual representations are called widget annotations and they are defined using an annotation dictionary.

Knowing this, your question needs to be rephrased: how do I change the location of a specific widget annotation of a specific field?

I've made a sample in Java named ChangeFieldPosition in answer to this question. It will be up to you to port it to C#.

You already have the AcroFields instance:

 AcroFields form = stamper.getAcroFields();

What you need now is the Item instance for the specific field (in my example: for the field with name "timezone2"):

Item item = form.getFieldItem("timezone2");

The position is a property of the widget annotation, so you need to ask the item for its widget. In the following line, I get the annotation dictionary for the first widget annotation (with index 0):

PdfDictionary widget = item.getWidget(0);

In most cases there will be only one widget annotation: each field has only one visual representation.

The position of the annotation is an array with four values: llx, lly, urx and ury. We can get this array like this:

PdfArray rect = widget.getAsArray(PdfName.RECT);

In the following line I change the x-value of the upper-right corner (index 2 corresponds with urx):

rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() - 10f));

As a result the width of the field is shortened by 10pt.