简体   繁体   中英

getAcroFields() get different fields with same name

I'm working with iText in a Java environment, and I want to recover different acrofields with the same tag-name on pdf.

This is my current code:

PdfStamper stamper = new PdfStamper(reader, output);
AcroFields fields = stamper.getAcroFields();
HashMap fieldsMap = fields.getFields();

fieldsMap just contain one value.
Is there a solution to have different acrofields with the same tag-name on pdf?

First let me explain that your question is technically wrong.

You can't have different fields with the same name. The name of a field is unique. One name corresponds with exactly one field. You can't have a field (eg with name fieldname ) that has different values.

However, one field can be represented by different widget annotations. For instance. A field named date can occur on every page to show a specific date in the header or footer of each page. The content of each of those widget annations will be the value of that field. You can't have two different widget annotations of the same field with a different content (although the displayed content can differ).

I'm sorry if this sounds confusion, but that's the way it's specified in the PDF standard.

Now for your question: Why are you asking?

If you want to get the position of the widget annotations of a specific field, you can ask for those specification using an index.

For instance, this code will give you the page number and coordinates of the field with index 0 :

PdfStamper stamper = new PdfStamper(reader, output);
AcroFields fields = stamper.getAcroFields();
int page = form.getFieldPositions(name).get(0).page;
Rectangle rectangle = form.getFieldPositions(name).get(0).position;

See Find field absolute position and dimension by acrokey

If you want to get properties of a field, you could use the getMerged() method. For instance, this code allows you to get a dictionary with properties of the first widget annotation of each field:

Map<String,AcroFields.Item> fields = form.getFields();
AcroFields.Item item;
PdfDictionary dict;
for (Map.Entry<String,AcroFields.Item> entry : fields.entrySet()) {
    out.write(entry.getKey());
    item = entry.getValue();
    dict = item.getMerged(0);
    // inspect dict
}

See finding out required fields to fill in pdf file

Or, once you have an Item object, you can get the getWidgets() method to get all the widgets:

dict = item.getWidgets().get(0).getPdfObject();

See How to get AcroField Properties using iText?

If you're asking for a different reason, please clarify.

Extra info:

The concept of a field and the concept of a widget annotation are different. A field is described in a field dictionary . A widget annotation is described in an annotation dictionary . One field dictionary (describing a specific field in a form) corresponds with one or more widget dictionaries (describing how the field is rendered once or multiple times).

If a field corresponds with only one widget dictionary, all the entries of both different dictionaries are often bundled into one and the same dictionary.

The getMerged() method results in a dictionary that contains the entries of both the field dictionary and the widget annotation dictionary.

I'm sorry if all of this sounds confusion. I've tried to do my best to explain this as simple as possible. Please comment if something is not entirely clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM