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?
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
PdfAcroForm instance, now you have to get the form fields:
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map fields = form.getFormFields();
What you need now is the
PdfFormField instance for the specific field (in my example: for the field with name
PdfFormField field = fields.get("timezone2");
The position is a property of the widget annotation, so you need to ask the
field for its widget. In the following line, I get the annotation dictionary for the first widget annotation (with index
PdfWidgetAnnotation widgetAnnotation = field.getWidgets().get(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:
ury. We can get this array like this:
PdfArray annotationRect = widgetAnnotation.getRectangle();
In the following line I change the x-value of the upper-right corner (index
2 corresponds with
annotationRect.set(2, new PdfNumber(annotationRect.getAsNumber(2).floatValue() - 10f));
As a result the width of the field is shortened by 10pt.
Click How to move an AcroForm field? if you want to see how to answer this question in iText 5.