简体   繁体   English

如何使用iText / Sharp从AcroFields获取TextField?

[英]How do I get a TextField from AcroFields using iText/Sharp?

I am using iTextSharp to loop through the fields in the AcroFields collection so I can set a various properties on an annotation. 我正在使用iTextSharp遍历AcroFields集合中的字段,因此可以在注释上设置各种属性。 I have worked out how to pull most of the properties for each of the annotation fields, but would like to be able to cast the individual annotation to the correct field object (ie, TextField , PushButtonField , RadioCheckField , etc.). 我已经研究出如何为每个注释字段提取大多数属性,但是希望能够将单个注释转换为正确的字段对象(即TextFieldPushButtonFieldRadioCheckField等)。

Short of creating a new TextField , reading and then setting all of the settings/properties associated with it, is there a concise way of getting to: 除了创建一个新的TextField ,读取然后设置与之关联的所有设置/属性之外,还有一种简洁的方法可以达到:

int index = 0;
AcroFields acroFields = stamper.AcroFields;
TextField tf = acroFields.GetTextField(acroField.Key.ToString(), index);

I am using a very old version of iTextSharp (4.0.6.0). 我使用的是iTextSharp(4.0.6.0)的旧版本。 I am unable to upgrade to the latest version as there are breaking changes between 4 and 5. 我无法升级到最新版本,因为4到5之间有重大更改。

Additional Information: My PDF files have multiple repeated fields (eg, two pages have the customer name), so setting a property by using just a key name can have unintended side effects. 附加信息:我的PDF文件具有多个重复字段(例如,两个页面都有客户名称),因此仅使用键名来设置属性可能会产生意想不到的副作用。 One field might be left justified while another is centered. 一个字段可能被对齐,而另一个字段居中。

不幸的是,不, TextFieldPushButtonField和其他元素都是iText用于文档创建的抽象的一部分,并且没有内置的方法可以将AcroFields.Item对象恢复为其中之一。

You can use the GetFieldType() while iterating the AcroFields . 您可以在迭代AcroFields时使用GetFieldType() But not all properties are available to change. 但并非所有属性都可以更改。 Let me know if there are any questions. 让我知道是否有任何问题。

AcroFields acroFields = reader.AcroFields;
foreach (KeyValuePair<String, AcroFields.Item> field in acroFields.Fields)
{
    // Check to see if it is the type we want.
    Boolean isTextField = (AcroFields.FIELD_TYPE_TEXT == acroFields.GetFieldType(field.Key));

    if (isTextField)
    {
        // Change the text.
        acroFields.SetField(field.Key, "new  string");
    }
}

The constant int field types available are: 可用的常量int字段类型为:

public const int FIELD_TYPE_CHECKBOX = 2;
public const int FIELD_TYPE_COMBO = 6;
public const int FIELD_TYPE_LIST = 5;
public const int FIELD_TYPE_NONE = 0;
public const int FIELD_TYPE_PUSHBUTTON = 1;
public const int FIELD_TYPE_RADIOBUTTON = 3;
public const int FIELD_TYPE_SIGNATURE = 7;
public const int FIELD_TYPE_TEXT = 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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