iTextSharp - move Acrofield

坚强是说给别人听的谎言 提交于 2019-12-05 12:32:10

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# (maybe you can post the C# answer here for further reference).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!