How to return the date format of a date form field?

@douglasdallas
Are you working with a license?

Check how a simpler version of the code works for you.

@sergei.shibanov

Yes, the license is being used. Iā€™ve tried the simplified version (as near as via Java) and it doesnā€™t work for me.

Have you tried in Java with my example and the attached PDF file and you are not seeing the issue?

Thanks for your help.

 /*
            var doc = new Document(dataDir + "form1.pdf");
            TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
            Console.WriteLine($"FullName: {tb.FullName}");
            tb.Value = "10-Feb-24";

            Console.WriteLine($"Result value: {tb.Value}");

            doc.Save(dataDir + "Filled-out.pdf");
         */

        Document setValueDocument = new Document("/tmp/form_with_field_formatting.pdf");

        Field fill_formatted_date1 = setValueDocument.getForm().getFields()[0];
        System.out.printf("FullName: %s\n", fill_formatted_date1.getFullName());
        fill_formatted_date1.setValue("16-Feb-24");

        String theValue = fill_formatted_date1.getValue();
        System.out.printf("Result value: %s\n", theValue);

        setValueDocument.save("out_simple.pdf");

Not yet, I mostly work in C#.
If necessary, I will do some reproducing in Java, but it will take time.
Still a question - are you using the latest version of the library (24.1 because changes for working with months as strings were added later than getECMAScriptString() on 23.12)?

We are using 2023.12; I will download 2024.01 and try to see if that resolves. Thanks.

@sergei.shibanov

It works in 2024.01 - the simple approach of hard coding a value - Iā€™lll see if it works when in our product.

To confirm; what string format for the date should be set? As itā€™s not a date object - I donā€™t see how it can work with all the display formats?

The customer expects the date to the formatted as per the defined display date.

For example if itā€™s a date only, should it always be set as ā€˜MM-dd-yyyyā€™?

Or should we take the formatting that has been defined as part of the ECMA script and use it? This presents a problem as this date format string is Javascript which is different from Java - so it would need converted somehow.

Thanks

@douglasdallas
I didnā€™t really understand your question.
Text fields according to the Pdf specification can contain ECMA scripts (java scripts). Which for OnFormat in some way influence (convert) the entered value. The entered data may be formatted as numbers with a certain format, dates, etc.
This can be set, for example, in Acrobat in the text field options.

@sergei.shibanov

Sorry, not the best explaination.

How about, from another angle, I have a value in our database which is a date field - and I would like to populate the form field in the PDF. To do this, using your API I need to set the value as a string.

What should the string value be set to, so that it works with any of the display values set within Adobe?

@douglasdallas
Thank you, now I understand your question.
If you are working with a document field about which you donā€™t know what parameters it has, you can:

  • Read the format and adapt to it
    To remove the AFDate_FormatEx(ā€œā€) wrapper you can use for example this code
int len = "AFDate_FormatEx(\"".Length;
int formatLen = jsString.Length;
string format = jsString.Remove(0, len).Remove(formatLen - len - 2, 2);
  • Reset OnFormat for fields and assign Value to strings representing the date in any format (just as strings, without formatting in the pdf itself).
var doc = new Document(dataDir + "form1.pdf");
TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
Console.WriteLine($"FullName: {tb.FullName}");

// Set Action.OnFormat to null.
tb.Actions.OnFormat = null;
tb.Value = "Any string";
Console.WriteLine($"Result value: {tb.Value}");
doc.Save(dataDir + "FilledWithAnyString.pdf");
  • Set the OnFormat field that you are using - but thereā€™s something unclear here, Iā€™ll figure it out and write it down.
var doc = new Document(dataDir + "form1.pdf");
TextBoxField tb = doc.Form.Fields[6] as TextBoxField;
Console.WriteLine($"FullName: {tb.FullName}");

// Set Action.OnFormat to the desired format.
// Not worked ?
string ECMAScriptString = "AFDate_FormatEx(\"dd/m/yy\")";

tb.Value = "";
tb.Actions.OnFormat = new JavascriptAction(ECMAScriptString);
tb.Value = "19 02 2024";
Console.WriteLine($"Result value: {tb.Value}");
doc.Save(dataDir + "FilledWithAnyString.pdf");

@sergei.shibanov

Thanks for your reply. Iā€™m not sure I understand it fully.

Are your steps:

  1. Remove the existing formatting
  2. Set the value in any string format
  3. Reinstate the old formatting

If so, how does Adobe know how to apply the formatting on the date when we re-add it, if it only has the string value and doesnā€™t know which part of the day, month or year?

not steps, variants
Variant 1. Find out the date format for the text field and generate the assigned string in accordance with this format.

Variant 2. Clear the formatting for the text field and after that you can assign any string to it.

Variant 3. Set some date format for the field and, knowing it, form a suitable string. (Iā€™ll have to check it again and if it doesnā€™t work Iā€™ll assign a task to the development team).

Ah, gottcha - thanks!

@douglasdallas
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56612

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.