使用iText设置新创建的复选框的导出值 [英] Set export value of a new created checkbox with iText

查看:168
本文介绍了使用iText设置新创建的复选框的导出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在现有的pdf中添加一个复选框。另外,我需要设置此复选框的导出值。我看到这应该配置外观字典/ AP,但到目前为止我确实评估了如何在一个新的复选框上设置它。
使用导出值我的意思是屏幕截图中标记的值

I need to add a checkbox to an existing pdf. Also I need set the export value of this checkbox. I saw that this should be configurared with appearance dictionary /AP but till now I did evaluate how to set this on a new checkbox. With export value I mean the value which is marked in the screenshot

这是我的代码,它创建了一个复选框,但导出值始终为空...

Here is my code which creates a checkbox, but the export value is always empty...

case AcroFields.FIELD_TYPE_CHECKBOX:
            PdfFormField checkbox = PdfFormField.createCheckBox(stamper.getWriter());
            checkbox.setWidget(fieldVO.rect, PdfAnnotation.HIGHLIGHT_NONE);
            checkbox.setFieldName(fieldVO.getNewName());
            //TODO set export value
            log.info("exportValue is " + fieldVO.getExportValue());
            stamper.addAnnotation(checkbox, fieldVO.getPageNumber());
            break;


推荐答案

首先:你使用的是 PdfFormField 创建一个复选框。这很难。为什么不使用 RadioCheckField 类?

First this: you are using PdfFormField to create a check box. That is hard. Why don't you use the RadioCheckField class?

rect = new Rectangle(180, 806, 200, 788);
checkbox = new RadioCheckField(stamper.getWriter(), rect,
     "fieldVO.getNewName()", "on");
field = checkbox.getCheckField();

然后你要定义外观。假设 onOff 是一个 PdfAppearance 对象的数组,如果没有选中该框,则为一个外观( 0 )如果选中此框,则为1( 1 )。请注意,关闭状态的名称应为关闭,如ISO-32000-1中所定义。据我所知,标准建议在 on state 上使用,但您可以使用自定义值,例如MyCustomValue

Then you want to define the appearances. Suppose that onOff is an array of PdfAppearance objects, one with the appearance if the box isn't selected (0) and one if the box is selected (1). Note that the name of the off state should be "Off" as defined in ISO-32000-1. As far as I remember, the standard recommends using "Yes" for the on state, but you can use a custom value, such as "MyCustomValue":

field.setAppearance(
    PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
field.setAppearance(
    PdfAnnotation.APPEARANCE_NORMAL, "MyCustomValue", onOff[1]);

在这种情况下, on state 的外观将存储为键入 / MyCustomValue 的条目。此密钥是 PDF名称对象,并且适用了一些限制。这就是为什么引入了 / opt 键:

In this case, the appearance of the on state will be stored as an entry with key /MyCustomValue. This key is a PDF name object and several restrictions apply. That's why the /Opt key was introduced:


(取自ISO-32000) -1)从PDF 1.4开始,复选框和单选按钮的字段字典可能包含可选的选项条目。如果存在, Opt 条目应是一个文本字符串数组,表示字段中每个注释的导出值。它可用于以下目的:

(Taken from ISO-32000-1) Beginning with PDF 1.4, the field dictionary for check boxes and radio buttons may contain an optional Opt entry. If present, the Opt entry shall be an array of text strings representing the export value of each annotation in the field. It may be used for the following purposes:


  • 表示非拉丁文书写系统中复选框和单选按钮字段的导出值。由于外观字典中的名称对象仅限于PDFDocEncoding,因此它们不能表示非拉丁文本。

  • 允许单独检查单选按钮或复选框,即使它们具有相同的导出值。

示例:一组复选框可能在多个页面上重复,这样所需的行为就是当用户检查一个框,还检查每个其他页面上的相应框。在这种情况下,每个相应的复选框都是复选框字段的Kids数组中的小部件。

EXAMPLE: a group of check boxes may be duplicated on more than one page such that the desired behavior is that when a user checks a box, the corresponding boxes on each of the other pages are also checked. In this case, each of the corresponding check boxes is a widget in the Kids array of a check box field.

在您的情况下,你需要类似的东西:

In your case, you'd need something like:

PdfArray options = new PdfArray();
options.add(new PdfString("My Custom Value"));
field.put(PdfName.OPT, options);

我还没有测试过这是否有效,我只是在解释ISO-32000-1所说的内容并将规范转换为iText代码。

I haven't tested if this works, I'm merely interpreting what ISO-32000-1 says and converting the spec into iText code.

最后,您可以添加该字段。

Finally, you can add the field.

stamper.addAnnotation(field, 1);

这篇关于使用iText设置新创建的复选框的导出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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