使用 Apache PDFBox 读取/写入日期值



我对使用apache pdfbox读取/写入日期值有一些疑问。

  1. 如何确定字段是日期字段(需要日期值(?

  2. 是否可以读取 pdf 字段的格式(例如 dd/mm/yyyy(格式?我找不到办法做到这一点。我在字段的COSDictionary中看到格式,但它是javascript调用的一部分。

  3. 如何正确设置日期字段的值?

提前非常感谢。

正如 mkl 所指出的,字段格式由 JavaScript 代码定义。我能够获得与之关联的格式的方法如下:

String js = Optional.ofNullable(acroForm.getField(fieldName)).map(PDField::getCOSObject)
        // Additional-actions dictionary. Defining the actions to be taken in response to various trigger events.
        .map(d -> (COSDictionary) d.getDictionaryObject(COSName.AA))
        // F dictionary. A JavaScript action to be performed before the field is formatted to display its current value.
        .map(d -> (COSDictionary) d.getDictionaryObject(COSName.F))
        // JS string. A string or stream containing the JavaScript script to be executed.
        .map(d -> d.getString(COSName.JS))
        .orElse(null);
Matcher m = Pattern.compile("AFDate_FormatEx\s*\(([^)]*)\)").matcher(js);
m.find();
String dateFormat = m.group(1);

然后需要对其进行解析,因为PDF日期格式不等于Java日期格式。

最新更新