Bernie
Bernie

Reputation: 1651

PDFTron: What is the proper way to find date fields in a PDF form

[PdfTron 5.2] I have a PDF form with text and date fields. I want to find the date fields. I can get the actions of the field with getActions().

field.getActions()

returns

{"F":[{"yc":"JavaScript","Gt":"AFDate_FormatEx(\"dd.mm.yyyy\");"}],
"K":[{"yc":"JavaScript","Gt":"...);","ey":null}]}

As you can see, the date is in actions.F[0].Gt. But checking actions.F[0].Gt for "AFDate" seems wrong, that's too low-level.

Is there a better API function to find out, that I have a date field?

Thank you.

Upvotes: 0

Views: 516

Answers (1)

Andy
Andy

Reputation: 131

You are correct. The Gt property is obfuscated and minified which is volatile and not meant to be used. If you require an API, you should refer to our documentation. Everything should be available there except a few (one of which will be used below), but feel free to contact us if you do need help!

Unfortunately, there is no API currently to get that type. From my limited understanding, the "type" of a field is determined by the attached actions and not simply a specific type or flag. This suggests all fields are just text fields with special formatting actions to make it look and feel like its a date or numeric field.

In this case, you will have to check the formatting action (F) as you have already noticed for the date formatting function (AFDate_FormatEx). To get the JavaScript from that action, you should use the javascript property on the action which was not in the documentation. However, you can see it if you console log the action.

Here is an example:

const dateActionJs = /.+:"AFDate_FormatEx\(.*/;

instance.docViewer.on('annotationsLoaded', () => {
  const annotations = annotManager.getAnnotationsList();
  annotations.forEach(annot => {
    const actions = annot.getField().getActions();
    if (actions.F && actions.F[0] && actions.F[0].javascript && dateActionJs.test(actions.F[0].javascript)) { // F = Format Action
      console.log('Found Date');
    }
  });
});

Let me know if this helps!

EDIT: You can search for AFDate instead of AFDate_FormatEx which will be sufficient.

Upvotes: 2

Related Questions