iText获取字段中的字符数 [英] iText Get number of characters in a field

查看:360
本文介绍了iText获取字段中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出哪个格式的出生日期应该是使用iText在PDF中的空白字段,

I am trying to find out which formats a date of birth should be in a empty field in a PDF using iText,

我可以用一个值标记该字段但后来需要知道出生​​日期必须是什么。

I can stamp the field with a value but then need to know what the date of birth must be.

我想如果我走出场地的长度,那么我知道格式应该如何,因为格式可以是:

I figured that if I get out the length of the field so I know how the format should be, because the formats can be:

YYYYMMDDNNNN(14位数)

YYYYMMDDNNNN (14 digits)

YYYYMMDD(10位数)

YYYYMMDD (10 digits)

YYMMDD(8位)

YYMMDD (8 digits)

该字段有固定的位数,如果我在字段上标记的数字过多,则超出该字段的数字消失。

The field has a fixed number of digits, if I stamp the field with too many numbers, then the numbers that fall outside the field disappears.

如何找到空字段的长度?

我如何盖章:

private void stampValuesOnFields()
{
  try
  {
    PdfReader reader = new PdfReader(System.getProperty("com.sun.aas.instanceRoot") + "/PDF/Templates/org.pdf");
    String path = System.getProperty("com.sun.aas.instanceRoot") + "/PDF/Generated PDF/" + "new.pdf";
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path));

    stamper.getAcroFields().setField("field", "19321029");
  }catch(Exception e)
  {}
}

以下是我尝试填写的pdf文件,但是我想让它适用于其他格式为生日的其他pdf文件:

Here are pdfs I try to fill in, but I want it to work for other pdf files with other formats of Birthday:

http://www.korkortsportalen.se/upload/dokument/blanketter/foretag/tstrk1031_lakarintyg_diabetes.pdf
(Sökandenspersonnummer下的字段)

http://www.korkortsportalen.se/upload/dokument/blanketter/foretag/tstrk1031_lakarintyg_diabetes.pdf ( the field under "Sökandens personnummer" )

推荐答案

你很幸运。您的字段定义 / MaxLen ,这意味着您可以找到字段的最大长度。在下面的屏幕截图中,您可以看到字段 Falt__41 的字段/注释字典的属性(可用于添加Sökandenspersonnummer ):

You're in luck. Your field defines a /MaxLen which means that you can find out the maximum length of the field. In the following screen shot, you can see the properties of the field / annotation dictionary for field Falt__41 (which can be used to add the Sökandens personnummer):

如您所见,此字段最多可包含12个字符。此外, / Ff (字段标志)值为29360128或二进制值:1110000000000000000000000。这意味着以下标志处于活动状态:不拼写检查不滚动梳理。无论您输入什么,梳子标志都会使字符均匀分布在可用宽度上。在这种情况下,每个字符将占用可用宽度的1/12。

As you can see, this field can contain a maximum of 12 characters. Moreover, the /Ff (fields flags) value is 29360128 or binary value: 1110000000000000000000000. This means that the following flags are active: do not spell check, do not scroll, and comb. The comb flag makes that whatever you enter, the characters will be evenly distributed over the available width. In this case, every character will take 1/12 of the available width.

现在如何检索 / MaxLen 价值?这或多或少在我的书中解释过,但我在内存中写了以下几行:

Now how do you retrieve the /MaxLen value? That's more or less explained in my book, but I'm writing the following lines from memory:

AcroFields form = reader.getAcroFields();
AcroFields.Item item = form.getFieldItem("Falt__41");
PdfDictionary field = item.getMerged(0);
PdfNumber maxlen = field.getAsNumber(PdfName.MAXLEN);

现在你可以得到 int 的值 maxlen

重要说明:并非每个字段都有 / MaxLen value。

Important note: not every field has a /MaxLen value.

这篇关于iText获取字段中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆